Search in sources :

Example 1 with BBRouter

use of com.bob.vertx.swagger.BBRouter in project vertx-swagger by bobxwang.

the class SwaggerApp method Run.

public static AnnotationConfigApplicationContext Run(Class<?> clasz) {
    final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(clasz);
    BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(ApplicationContextHolder.class);
    applicationContext.registerBeanDefinition("applicationContextHolder", bdb.getBeanDefinition());
    // Just For Init
    applicationContext.getBean(ApplicationContextHolder.class);
    final Environment environment = applicationContext.getBean(Environment.class);
    final Vertx vertx = applicationContext.getBean(Vertx.class);
    final SpringVerticleFactory verticleFactory = new SpringVerticleFactory();
    vertx.registerVerticleFactory(verticleFactory);
    try {
        applicationContext.getBean(Router.class);
    } catch (BeansException be) {
        if (be instanceof NoSuchBeanDefinitionException) {
            Router rr = new RouterImpl(vertx);
            applicationContext.getBeanFactory().registerSingleton("router", rr);
        }
    }
    final Router router = applicationContext.getBean(Router.class);
    initSwagger(environment);
    configRouter(vertx, router, environment);
    Map<String, Verticle> maps = applicationContext.getBeansOfType(Verticle.class);
    DeploymentOptions options = new DeploymentOptions().setInstances(1).setWorker(true);
    for (Map.Entry<String, Verticle> temp : maps.entrySet()) {
        Verticle verticle = temp.getValue();
        String name = verticle.getClass().getSimpleName().substring(0, 1).toLowerCase() + verticle.getClass().getSimpleName().substring(1);
        vertx.deployVerticle(verticleFactory.prefix() + ":" + name, options);
        for (Method method : verticle.getClass().getDeclaredMethods()) {
            if (method.isAnnotationPresent(Override.class)) {
                continue;
            }
            if (method.getName().contains("lambda")) {
                continue;
            }
            if (io.swagger.util.ReflectionUtils.isOverriddenMethod(method, verticle.getClass())) {
                continue;
            }
            if (method.isAnnotationPresent(BBRouter.class)) {
                BBRouter bbRouter = io.swagger.util.ReflectionUtils.getAnnotation(method, BBRouter.class);
                Route route = router.route(bbRouter.httpMethod(), bbRouter.path());
                route.handler(ctx -> {
                    ReflectionUtils.makeAccessible(method);
                    ReflectionUtils.invokeMethod(method, verticle, ctx);
                });
            }
        }
    }
    HttpServer httpServer = vertx.createHttpServer(new HttpServerOptions().setSsl(false).setKeyStoreOptions(new JksOptions().setPath("server-keystore.jks").setPassword("secret")));
    httpServer.requestHandler(router::accept);
    int port;
    try {
        port = Integer.valueOf(environment.getProperty("server.port", "8080"));
    } catch (Exception e) {
        throw new RuntimeException("请配置有效端口号");
    }
    httpServer.listen(port, ar -> {
        if (ar.succeeded()) {
            logger.info("Server started on port " + ar.result().actualPort());
        } else {
            logger.error("Cannot start the server: " + ar.cause());
        }
    });
    return applicationContext;
}
Also used : BBRouter(com.bob.vertx.swagger.BBRouter) HttpServerOptions(io.vertx.core.http.HttpServerOptions) RouterImpl(io.vertx.ext.web.impl.RouterImpl) HttpServer(io.vertx.core.http.HttpServer) Route(io.vertx.ext.web.Route) BeansException(org.springframework.beans.BeansException) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Router(io.vertx.ext.web.Router) BBRouter(com.bob.vertx.swagger.BBRouter) Method(java.lang.reflect.Method) HttpMethod(io.vertx.core.http.HttpMethod) Vertx(io.vertx.core.Vertx) BeansException(org.springframework.beans.BeansException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Verticle(io.vertx.core.Verticle) DeploymentOptions(io.vertx.core.DeploymentOptions) JksOptions(io.vertx.core.net.JksOptions) Environment(org.springframework.core.env.Environment) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Map(java.util.Map)

Example 2 with BBRouter

use of com.bob.vertx.swagger.BBRouter in project vertx-swagger by bobxwang.

the class RestApiVerticle method handle.

@BBRouter(path = "/catalogue/products/:producttype/:productid", httpMethod = HttpMethod.GET)
@ApiImplicitParams({ @ApiImplicitParam(name = "producttype", value = "产品类型", required = true, dataType = "string", paramType = "path"), @ApiImplicitParam(name = "productid", value = "产品标识", dataType = "string", paramType = "path") })
private void handle(RoutingContext ctx) {
    String productType = ctx.request().getParam("producttype");
    String productID = ctx.request().getParam("productid");
    ctx.response().putHeader("content-type", "application/json;charset=UTF-8").end(new JsonObject().put("now", new Date().toString()).put("producttype", productType).put("productid", productID).encodePrettily());
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Date(java.util.Date) BBRouter(com.bob.vertx.swagger.BBRouter) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams)

Example 3 with BBRouter

use of com.bob.vertx.swagger.BBRouter in project vertx-swagger by bobxwang.

the class UserVerticle method postUser.

@BBRouter(path = "/user/", httpMethod = HttpMethod.POST)
@ApiOperation(value = "创建一个用户", response = UserRes.class)
@ApiImplicitParams({ @ApiImplicitParam(name = "query", value = "用户体", dataType = "com.bob.vertx.webapi.param.req.UserReq", paramType = "body") })
private void postUser(RoutingContext ctx) {
    logger.info(Thread.currentThread().getName());
    JsonObject jsonObject = ctx.getBodyAsJson();
    jsonObject.put("now", new Date().toString());
    ctx.response().putHeader("content-type", "application/json;charset=UTF-8").end(Json.encodePrettily(jsonObject));
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Date(java.util.Date) BBRouter(com.bob.vertx.swagger.BBRouter)

Example 4 with BBRouter

use of com.bob.vertx.swagger.BBRouter in project vertx-swagger by bobxwang.

the class UserVerticle method updateUserById.

@BBRouter(path = "/user/:id", httpMethod = HttpMethod.PUT)
@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户标识", required = true, dataType = "integer", paramType = "path"), @ApiImplicitParam(name = "query", value = "用户体", dataType = "com.bob.vertx.webapi.param.req.UserReq", paramType = "body") })
@ApiOperation(value = "更新一个用户", response = UserRes.class)
@ApiResponses({ @ApiResponse(message = "response_annotation1", code = 200, response = UserRes.class) })
private void updateUserById(RoutingContext ctx) {
    UserReq userReq = Json.decodeValue(ctx.getBody(), UserReq.class);
    UserRes userRes = new UserRes();
    userRes.setId(Integer.valueOf(ctx.request().getParam("id")));
    userRes.setName(userReq.getName() + " updated");
    ctx.response().putHeader("content-type", "application/json;charset=UTF-8").end(Json.encodePrettily(userRes));
}
Also used : UserReq(com.bob.vertx.webapi.param.req.UserReq) UserRes(com.bob.vertx.webapi.param.res.UserRes) BBRouter(com.bob.vertx.swagger.BBRouter)

Aggregations

BBRouter (com.bob.vertx.swagger.BBRouter)4 JsonObject (io.vertx.core.json.JsonObject)2 Date (java.util.Date)2 UserReq (com.bob.vertx.webapi.param.req.UserReq)1 UserRes (com.bob.vertx.webapi.param.res.UserRes)1 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 Verticle (io.vertx.core.Verticle)1 Vertx (io.vertx.core.Vertx)1 HttpMethod (io.vertx.core.http.HttpMethod)1 HttpServer (io.vertx.core.http.HttpServer)1 HttpServerOptions (io.vertx.core.http.HttpServerOptions)1 JksOptions (io.vertx.core.net.JksOptions)1 Route (io.vertx.ext.web.Route)1 Router (io.vertx.ext.web.Router)1 RouterImpl (io.vertx.ext.web.impl.RouterImpl)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 BeansException (org.springframework.beans.BeansException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1