Search in sources :

Example 1 with Route

use of io.vertx.ext.web.Route 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 Route

use of io.vertx.ext.web.Route in project Summer by yale8848.

the class SummerRouter method registerResource.

public void registerResource(Class clazz) {
    if (isRegister(clazz)) {
        return;
    }
    MethodsProcessor.get(classInfos, clazz);
    for (ClassInfo classInfo : classInfos) {
        for (MethodInfo methodInfo : classInfo.getMethodInfoList()) {
            String p = classInfo.getClassPath() + methodInfo.getMethodPath();
            p = PathParamConverter.converter(p);
            p = addContextPath(p);
            Route route = null;
            if (methodInfo.getHttpMethod() == null) {
                route = router.route(p);
            } else if (methodInfo.getHttpMethod() == GET.class) {
                route = router.get(p);
            } else if (methodInfo.getHttpMethod() == POST.class) {
                route = router.post(p);
            } else if (methodInfo.getHttpMethod() == PUT.class) {
                route = router.put(p);
            } else if (methodInfo.getHttpMethod() == DELETE.class) {
                route = router.delete(p);
            } else if (methodInfo.getHttpMethod() == OPTIONS.class) {
                route = router.options(p);
            } else if (methodInfo.getHttpMethod() == HEAD.class) {
                route = router.head(p);
            }
            if (methodInfo.isBlocking()) {
                route.blockingHandler(getHandler(classInfo, methodInfo));
            } else {
                route.handler(getHandler(classInfo, methodInfo));
            }
        }
    }
}
Also used : MethodInfo(ren.yale.java.method.MethodInfo) Route(io.vertx.ext.web.Route) ClassInfo(ren.yale.java.method.ClassInfo)

Example 3 with Route

use of io.vertx.ext.web.Route in project vertx-web by vert-x3.

the class TemplateTest method testRelativeToRoutePath.

private void testRelativeToRoutePath(String pathPrefix) throws Exception {
    TemplateEngine engine = new TestEngine(false);
    router.route().handler(context -> {
        context.put("foo", "badger");
        context.put("bar", "fox");
        context.next();
    });
    Route route = router.route();
    if (pathPrefix != null) {
        route.path(pathPrefix + "*");
    }
    route.handler(TemplateHandler.create(engine, "somedir", "text/html"));
    String expected = "<html>\n" + "<body>\n" + "<h1>Test template</h1>\n" + "foo is badger bar is fox<br>\n" + "</body>\n" + "</html>";
    testRequest(HttpMethod.GET, pathPrefix != null ? pathPrefix + "/test-template.html" : "/test-template.html", 200, "OK", expected);
}
Also used : Route(io.vertx.ext.web.Route)

Example 4 with Route

use of io.vertx.ext.web.Route in project vertx-web by vert-x3.

the class TemplateHandlerImplTest method testTurnOffIndex.

@Test
public void testTurnOffIndex() {
    TemplateEngine templateEngine = mock(TemplateEngine.class);
    RoutingContext routingContext = mock(RoutingContext.class);
    when(routingContext.normalisedPath()).thenReturn("/");
    Route currentRoute = mock(Route.class);
    when(currentRoute.getPath()).thenReturn("/");
    when(routingContext.currentRoute()).thenReturn(currentRoute);
    TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext");
    templateHandler.setIndexTemplate(null);
    templateHandler.handle(routingContext);
    verify(templateEngine).render(any(), eq("templates"), eq("/"), any());
}
Also used : TemplateEngine(io.vertx.ext.web.templ.TemplateEngine) TemplateHandlerImpl(io.vertx.ext.web.handler.impl.TemplateHandlerImpl) RoutingContext(io.vertx.ext.web.RoutingContext) TemplateHandler(io.vertx.ext.web.handler.TemplateHandler) Route(io.vertx.ext.web.Route) Test(org.junit.Test)

Example 5 with Route

use of io.vertx.ext.web.Route in project vertx-web by vert-x3.

the class TemplateHandlerImplTest method testSimpleTemplate.

@Test
public void testSimpleTemplate() {
    TemplateEngine templateEngine = mock(TemplateEngine.class);
    RoutingContext routingContext = mock(RoutingContext.class);
    when(routingContext.normalisedPath()).thenReturn("/about");
    Route currentRoute = mock(Route.class);
    when(currentRoute.getPath()).thenReturn("/");
    when(routingContext.currentRoute()).thenReturn(currentRoute);
    TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext");
    templateHandler.handle(routingContext);
    verify(templateEngine).render(any(), eq("templates"), eq("/about"), any());
}
Also used : TemplateEngine(io.vertx.ext.web.templ.TemplateEngine) TemplateHandlerImpl(io.vertx.ext.web.handler.impl.TemplateHandlerImpl) RoutingContext(io.vertx.ext.web.RoutingContext) TemplateHandler(io.vertx.ext.web.handler.TemplateHandler) Route(io.vertx.ext.web.Route) Test(org.junit.Test)

Aggregations

Route (io.vertx.ext.web.Route)30 JsonObject (io.vertx.core.json.JsonObject)17 ServerCacheException (com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException)14 UnknownAPIException (com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException)14 APICallException (com.arm.mbed.cloud.sdk.testutils.APICallException)14 MissingInstanceException (com.arm.mbed.cloud.sdk.testserver.cache.MissingInstanceException)13 RoutingContext (io.vertx.ext.web.RoutingContext)10 Router (io.vertx.ext.web.Router)7 HttpMethod (io.vertx.core.http.HttpMethod)4 AbstractVerticle (io.vertx.core.AbstractVerticle)3 Handler (io.vertx.core.Handler)3 HttpServerResponse (io.vertx.core.http.HttpServerResponse)3 TemplateHandler (io.vertx.ext.web.handler.TemplateHandler)3 TemplateHandlerImpl (io.vertx.ext.web.handler.impl.TemplateHandlerImpl)3 TemplateEngine (io.vertx.ext.web.templ.TemplateEngine)3 Date (java.util.Date)3 Map (java.util.Map)3 Set (java.util.Set)3 Test (org.junit.Test)3 APIMethodResult (com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult)2