开发一个小型的http服务

我们经常会遇到这种情况,使用tomcat感觉大了一点,使用原生的各种IO 又感觉复杂了一点。

所以会需要一些介于这两者之间的东西。 今天就来给大家分享一个开源项目,Magician

引入依赖

<dependency>
    <groupId>com.github.yuyenews</groupId>
    <artifactId>Magician</artifactId>
    <version>1.1.16</version>
</dependency>

<!-- This is the log package, which supports any package that can be bridged with slf4j -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.12</version>
</dependency>

创建handler

@TCPHandler(path="/")
public class DemoHandler implements TCPBaseHandler<MagicianRequest> {
 
    @Override
    public void request(MagicianRequest magicianRequest) {
        // response data
        magicianRequest.getResponse()
                .sendJson(200, "{'status':'ok'}");
    }
}

创建服务

创建一个tcp服务,设置成HTTP解码器即可

Magician.createTCPServer()
                    .scan("handler所在的包名")
                    .protocolCodec(new HttpProtocolCodec())
                    .bind(8080);

访问官网可以了解更多:http://magician-io.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章