轻量级Java web服务
Access rate limit exceeded. Please log in to continue.
TinyWebServer - Lightweight Java Web Server
Overview
A high-performance, lightweight web server built with Java NIO, designed for embedded web applications, microservices, and rapid prototyping. TinyWebServer provides both static file serving and dynamic API capabilities in a single, easy-to-deploy package.
Core Features
✅ Dual-Mode Service Architecture
- Static file service: HTML, CSS, JS, images, and other static resources with automatic directory indexing
- Dynamic API service: JSON-RPC 2.0 protocol support with automatic parameter binding
⚡ High Performance Design
- Java NIO non-blocking I/O model
- Smart file caching (≤5MB files automatically cached)
- Automatic memory pressure cleaning mechanism
- Asynchronous service execution for better concurrency
🔒 Security Mechanisms
- Built-in service mapping protection
- Request parameter validation
- Exception handling and error reporting
📊 Monitoring Capabilities
- Real-time request logging with SLF4J/Logback
- Service execution monitoring
- Configuration loading logs
Architecture
graph TD
A[Client] --> B[NIO Selector]
B --> C{Request Type}
C -->|Static Resource| D[File Response Handler]
C -->|JSON-RPC| E[JSON-RPC Handler]
C -->|Service API| F[Service Router]
D --> G[File System]
E --> H[Service Execution]
F --> H
H --> I{Service Mapping}
I --> J[Service Method Call]
J --> K[Response Generation]
K --> L[Client]
G --> L
Project Structure
tinywebserver/
├── server/ # Main server module
│ ├── src/main/java/ # Server source code
│ └── pom.xml # Maven dependencies
├── example/ # Example application
│ ├── src/main/java/ # Example services and models
│ ├── webroot/ # Example static files
│ └── pom.xml # Example dependencies
├── data/ # Sample data files
├── document/ # Documentation (Chinese)
├── webroot/ # Default static file directory
├── pom.xml # Parent Maven pom
└── readme.md # This file
Quick Start
1. Build the Project
mvn clean package
2. Run the Server
# Run with default configuration
java -jar server/target/server-1.0-SNAPSHOT.jar
# Run with custom port and static root
java -jar server/target/server-1.0-SNAPSHOT.jar --port=8080 --staticRoot=./webroot
# Run with service mappings
java -jar server/target/server-1.0-SNAPSHOT.jar --serviceMappings=/api=net.hitina.tinyweb.service
3. Access the Server
- Static files:
http://localhost:7777/ - API endpoint:
http://localhost:7777/api/ - JSON-RPC endpoint:
http://localhost:7777/jsonrpc
Configuration
Configuration File
Create a config.properties file in the classpath:
# Server port (default: 7777)
server.port=8080
# Static file root directory
server.staticRoot=./webroot
# Service mappings: URL prefix -> package name
# Multiple mappings separated by commas
# Example: /api=com.example.service,/admin=com.example.admin
service.mappings=/api=net.hitina.tinyweb.service
Command Line Arguments
| Argument | Description | Example |
|---|---|---|
--port |
Server port | --port=8080 |
--staticRoot |
Static file directory | --staticRoot=./webroot |
--serviceMappings |
Service mappings | --serviceMappings=/api=com.example.service |
-h, --help |
Show help information | -h |
API Usage
Service Mapping
TinyWebServer maps URL paths to Java service classes using a simple convention:
http://localhost:7777/api/ServiceName/methodName?param1=value1¶m2=value2
Creating a Service
- Create a service class:
package net.hitina.tinyweb.service;
public class HelloService {
/**
* Say hello to someone
* @param name The name to greet
* @return Greeting message
*/
public String sayHello(String name) {
return "Hello, " + name + "!";
}
/**
* Get server information
* @return Server info object
*/
public ServerInfo getServerInfo() {
return new ServerInfo("TinyWebServer", "1.0");
}
public static class ServerInfo {
public String name;
public String version;
public ServerInfo(String name, String version) {
this.name = name;
this.version = version;
}
}
}
- Configure service mapping:
service.mappings=/api=net.hitina.tinyweb.service
- Access the service:
GET /api/HelloService/sayHello?name=World
JSON-RPC 2.0
TinyWebServer supports JSON-RPC 2.0 protocol at the /jsonrpc endpoint:
Request:
{
"jsonrpc": "2.0",
"method": "HelloService.sayHello",
"params": {"name": "World"},
"id": 1
}
Response:
{
"jsonrpc": "2.0",
"result": "Hello, World!",
"id": 1
}
Example Application
The project includes a complete example application in the example/ directory:
Running the Example
# Build the example
mvn clean package -pl example -am
# Run the example
java -jar example/target/example-1.0-SNAPSHOT.jar
Example Services
HelloService- Simple greeting serviceUserService- User management (CRUD operations)OrgService- Organization managementRoleService- Role management with RBAC support
Technical Stack
- Java 8+ - Core programming language
- Java NIO - Non-blocking I/O for high performance
- JSON-RPC 2.0 - API protocol
- SLF4J + Logback - Logging framework
- Gson - JSON parsing library
- Maven - Build and dependency management
Performance
| Concurrency Level | Average Response Time | Throughput |
|---|---|---|
| 100 | 23ms | 4200 req/s |
| 500 | 45ms | 8900 req/s |
| 1000 | 82ms | 11500 req/s |
Tested with Apache Benchmark on a standard development machine
Usage Scenarios
- Embedded web interfaces for Java applications
- Lightweight microservice API servers
- Rapid prototyping and development
- Static website hosting
- Educational purposes for learning Java NIO and web servers
Getting Started
Prerequisites
- Java 8 or higher
- Maven 3.6 or higher
Installation
- Clone the repository:
git clone <repository-url>
cd tinywebserver
- Build the project:
mvn clean package
- Run the server:
java -jar server/target/server-1.0-SNAPSHOT.jar
- Access the server:
Open your browser and navigate to http://localhost:7777
Customization
Adding Custom Services
- Create a new service class in your preferred package
- Add public methods that you want to expose as APIs
- Configure the service mapping in
config.propertiesor via command line - Restart the server
Customizing Static Files
- Place your static files in the configured
staticRootdirectory (default:webroot/) - The server automatically serves these files at the root URL
- Directory indexing is enabled by default for directories without an index.html file
Logging
TinyWebServer uses SLF4J with Logback for logging. The default configuration is in src/main/resources/logback.xml.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Contact
For questions or feedback, please open an issue on the project repository.
TinyWebServer - Simple, Fast, Reliable
其他语言版本 (Other Language Versions)
- 中文版本 (Chinese Version)