# netty-server **Repository Path**: wlx0902/netty-server ## Basic Information - **Project Name**: netty-server - **Description**: 简化SpringBoot内部封装细节 - **Primary Language**: Java - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-09-12 - **Last Updated**: 2024-12-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 启动方式 ```java public static void main(String[] args) { //一行代码 Server.start(Main.class); } ``` 1.支持统一格式返回 ```java @ResponseHandler public class HandlerResponseDevice implements ResponseDevice { @Override public Object beforeBodyWrite(Object result) { System.out.println("------ 返回值包装器 -------"); return result; } } ``` 2.支持统一异常处理 ```java @ControllerExceptionAdvice public class GlobalExceptionDevice { @ExceptionHandler(ArithmeticException.class) public String arithmeticException() { System.out.println("ArithmeticException 异常处理"); return "arithmeticException"; } @ExceptionHandler(IndexOutOfBoundsException.class) public String indexOutOfBoundsException() { System.out.println("IndexOutOfBoundsException 异常处理"); return "indexOutOfBoundsException"; } } ``` 3.支持websocket功能 ```yaml webSocket: #是否开启websocket 支持 open: false #web socket 请求路径 url: /txt ``` 方法声明 ```java @Order(1) //遵循netty pipeline调用链,进行消息处理 public class LogHandler extends WebSocketHandler { @Override protected void received(ChannelHandlerContext ctx, WebSocketFrame frame) { System.out.println("log handler"); super.webSocketHandler().next(ctx, frame); } } ``` 4.标准Handler声明 强制接收参数为NettyHttpRequest (不添加无法注册方法 会有错误信息提示) 目前支持@GET和@POST 两个请求方法声明 ```java @Handler("test") public class TestHandler { @GET("/t1") public String test1(NettyHttpRequest request) { return "test1"; } } // 合法处理器方法会打印log提示 // 2024-10-22T17:22:09.972 [main] ================= GET /test/t1 ``` 5.支持请求转发和重定向 ```java //请求转发 URL url = new URL("https://www.baidu.com"); HTTP.forward(url, request); //重定向 HTTP.redirect("https://www.baidu.com"); ``` 6.支持动态路由声明 ```java @GET("/get/{id}") //编程式获取 Map pathVariable = request.getPathVariable(); String id = pathVariable.get("id"); ``` 7.支持前置鉴权拦截器 ```java @Before public class TestBeforeFilter implements BeforeFilter { @Override public void before(NettyHttpRequest request) { System.out.println("前置TestBeforeFilter中间件"); // ContextHolder.sendData("权限不足", HttpResponseStatus.UNAUTHORIZED); } //细粒度空值当前拦截器执行方法 默认include包含所有 @Override public List includePath() { return BeforeFilter.super.includePath(); } //排除include所包含的路径 @Override public List excludePath() { return Arrays.asList("/hello/m3/t1"); } ``` 8.下一步 @GET @POST 后续替换 JAX-WS ```java log.print("w") ``` 9.。。。。。。。