目前我在写一个简单的 mvc 框架,然后因为想实现的好玩一点,就参考了 biezhi 大哥的 blade,注册路由的操作可以是这样的
public class ExampleApplication { public static void main(String[] args) { Reverie.up() .get("/get", ctx -> { ctx.response().end("你好"); }) .start(ExampleApplication.class, args); } }
因为用的 Vertx,所以注册路由的操作比较简单:
public void get(String path, RouteHandler routeHandler) { Vertx vertx = Vertx.vertx(); Router route = Router.router(vertx); //这一步就是注册路由的操作,routeHandler就是上面启动函数中的ctx route.route(HttpMethod.GET, path).handler(routeHandler); vertx.createHttpServer().requestHandler(route::accept).listen(8080); }
目前我想做的就是,这个注册路由时的 handler 方法,传入的参数是 Handler,这个 RoutingContext 就是启动函数中的 ctx,我想自己重新定义一个 RouteContext,定义我自己的 api,比如,如果使用 RoutingContext,那么在 main 函数中返回一串文本就要这样写:
//RoutingContext get("/get", ctx -> { ctx.response().end("你好"); }) //RouteContext,我自己定义的api为 get("/get", ctx -> { ctx.text("你好"); })
这是 RouteHandler 接口
@FunctionalInterface public interface RouteHandler extends Handler<RoutingContext> { void handle(RoutingContext routingContext); }
如果修改为:
@FunctionalInterface public interface RouteHandler extends Handler<RouteContext> { void handle(RouteContext routingContext); }
但是在注册路由处理器的时候,出现:
类型不匹配这种,我尝试过继承或者实现 RoutingContext,都没有效果,
handle 方法的入参要去为 Handler<RoutingContext>
类型的,试过不同办法,不知道该咋办了。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于