use of io.vertx.ext.web.impl.RouterImpl 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;
}
Aggregations