flyingboot项目介绍基于Netty的轻量级web快速开发框架。 接入使用1. 添加maven依赖<dependency> <groupId>com.github.gaojh</groupId> <artifactId>flyingboot</artifactId> <version>${last-version}</version></dependency> 2. 添加项目配置文件flyingboot默认读取application.properties配置文件中的配置 所有配置项可以使用Environment来进行获取 application.properties默认参数如下,如果需要修改,可以在文件中重新定制: #httpserver端口,默认8080server.port=8080 3. 新建启动类类似于springboot的Application类 @ComponentScan({"com.github.gaojh.example"})public class FlyingbootDemo { public static void main(String[] args) { new Flying().port(12345).run(FlyingbootDemo.class); }} 如果不加@ComponentScan注解,则包扫描路径直接设置为启动的包路径。 4. 添加Controller@Controllerpublic class DemoController { @Autowired private DemoService demoService; @RequestMapping("/hello") public String hello(@RequestParam String name){ return demoService.getName(name); } @RequestMapping("/demo") public Object demo(@RequestBody DemoBean demoBean){ return demoBean; } @RequestMapping("/h2/*") public String h2(){ return "h2"; }} 到此时,一个简单的Flyingboot项目已经可以运行了,跟springboot很相似。 5. 如何使用过滤器import com.github.gaojh.ioc.annotation.WebFilter;import com.github.gaojh.mvc.filter.Filter;import com.github.gaojh.server.http.HttpContext;@WebFilter(pathPatterns = {"/**"}, ignorePathPatterns = {"/hello"}, order = 5)public class DemoInterceptor implements Filter { private static Logger logger = LoggerFactory.getLogger(DemoInterceptor.class); @Override public boolean doBefore(HttpContext httpContext) throws Exception { logger.info("demo"); return true; } @Override public void doAfter(HttpContext httpContext) throws Exception { logger.info("demo postHandle"); }} Filter,里面有三个方法 1、doBefore前置处理器,在调用业务方法之前调用,如果返回true,继续调用下个过滤器,如果返回false,则不调用下个过滤器。 2、doAfter后置处理器,在调用业务方法之后调用。 @WebFilter注解必须要加上,否则会扫描不到该过滤器。 1、pathPatterns是用于匹配过滤的url。 2、ignorePathPatterns是用户匹配忽略过滤的url。 3、order指定过滤器的顺序 6. 如何使用动态Controller1、首先定义Handler @Componentpublic class DemoDynamicHandler implements RouterHandler { @Override public Object handle(HttpRequest httpRequest) { return "ok"; }} 实现RouterHandler接口,实现handle方法,这个方法就是类似Controller中的方法是一样的。 2、其次添加动态路由到Flyingboot import com.github.gaojh.ioc.annotation.Setup;import com.github.gaojh.mvc.setup.SetupAdapter;@Setuppublic class FlyingSetup implements SetupAdapter { @Override public void run() { Routers.me.get("/taobao", httpRequest -> "123123123"); }} 这样就可以添加动态的routerRouters可以动态添加路由,非常适合动态路由使用场景@Setup用于启动后首次运行 7. 启用websocket在application.properties中添加配置 flying.websocket.enable=true 或者 public static void main(String[] args) { new Flying().port(11111).enableWebsocket(true).run(FlyingbootDemo.class);} 添加websocketHandler @Componentpublic class MyWebSocketHandler implements WebSocketHandler { @Override public void onHandshake(ChannelHandlerContext ctx, FullHttpRequest fullHttpRequest) throws Exception { System.out.println(fullHttpRequest.uri()); } @Override public void onMessage(ChannelHandlerContext ctx, String msg) throws Exception { System.out.println("msg: "+msg); sendMessage(ctx,"回复:"+msg); } @Override public void onClose(ChannelHandlerContext ctx) throws Exception { }} 这样websocket就可以使用了 8. 使用Aop相关example请参考flyingboot-test项目本项目还在不断的迭代中,欢迎关注并提出意见! |
请发表评论