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;
}
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());
}
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));
}
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));
}
Aggregations