如果你想用netty 開發http,但是又嫌它麻煩,那麼Magician應該是一個比較好的選擇

Magician是一個基於netty開發的http服務包,僅僅基於netty做了一個非常輕量的擴展,可以方便的創建handler, 支持http,websocket。

使用起來也很簡單

引入依賴

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

<!-- 這是日誌包,必須有,不然控制檯看不到東西,支持任意可以看slf4j橋接的日誌包 -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.12</version>
</dependency>

創建handler

@HttpHandler(path="/demo")
public class DemoHandler implements HttpBaseHandler {

    @Override
    public void request(MagicianRequest magicianRequest, MagicianResponse response) {
        // response data
        magicianRequest.getResponse()
                .sendJson(200, "{'status':'ok'}");
    }
}

啓動服務

Magician.createHttp()
                    .scan("handler所在的包名")
                    .bind(8080);

創建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) {

    }
}

然後正常的啓動服務即可

可以訪問官網瞭解更多:https://magician-io.com

 

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