Magician基于 Netty 开发的小型 Http 服务包
Magician 是一个基于Netty开发的小型 HTTP服务包,可以非常方便的启动一个http服务,同时也支持WebSocket,注解式Handler配置
运行环境
JDK8+
文档
示例
导入依赖
<dependency>
<groupId>com.github.yuyenews</groupId>
<artifactId>Magician</artifactId>
<version>2.0.7</version>
</dependency>
<!-- 这是日志包,必须有,不然控制台看不到东西,支持任意可以看slf4j桥接的日志包 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.12</version>
</dependency>
创建http服务
创建一个 Handler
@HttpHandler(path="/")
public class DemoHandler implements HttpBaseHandler {
@Override
public void request(MagicianRequest magicianRequest, MagicianResponse response) {
// response data
magicianRequest.getResponse()
.sendJson(200, "{'status':'ok'}");
}
}
创建http服务
Magician.createHttp()
.scan("handler所在的包名")
.bind(8080);
创建http服务,监听多端口
HttpServer httpServer = Magician.createHttp()
.setConfig(
MagicianConfig.create().setNumberOfPorts(2)
)
.scan("com.magician.test2");
httpServer.bind(8080);
httpServer.bind(8081);
创建 WebSocket
@WebSocketHandler(path = "/websocket")
public class DemoSocketHandler implements WebSocketBaseHandler {
@Override
public void onOpen(WebSocketSession webSocketSession) {
}
@Override
public void onClose(WebSocketSession webSocketSession) {
}
@Override
public void onMessage(String message, WebSocketSession webSocketSession) {
}
}
更多组件
可以利用这些组件方便的开发web项目
评论