Search in sources :

Example 16 with RouterFactoryOptions

use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.

the class OpenAPI3RouterFactoryTest method mountNotImplementedHandler.

@Test
public void mountNotImplementedHandler() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    OpenAPI3RouterFactory.create(this.vertx, "src/test/resources/swaggers/router_factory_test.yaml", openAPI3RouterFactoryAsyncResult -> {
        routerFactory = openAPI3RouterFactoryAsyncResult.result();
        routerFactory.setOptions(new RouterFactoryOptions().setRequireSecurityHandlers(false).setMountNotImplementedHandler(true));
        latch.countDown();
    });
    awaitLatch(latch);
    startServer();
    testRequest(HttpMethod.GET, "/pets", 501, "Not Implemented");
}
Also used : RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 17 with RouterFactoryOptions

use of io.vertx.ext.web.api.contract.RouterFactoryOptions in project vertx-web by vert-x3.

the class OpenAPI3Examples method mainExample.

public void mainExample(Vertx vertx) {
    // Load the api spec. This operation is asynchronous
    OpenAPI3RouterFactory.create(vertx, "src/main/resources/petstore.yaml", openAPI3RouterFactoryAsyncResult -> {
        if (openAPI3RouterFactoryAsyncResult.succeeded()) {
            // Spec loaded with success, retrieve the router
            OpenAPI3RouterFactory routerFactory = openAPI3RouterFactoryAsyncResult.result();
            // You can enable or disable different features of router factory through mounting RouterFactoryOptions
            // For example you can enable or disable the default failure handler for ValidationException
            RouterFactoryOptions options = new RouterFactoryOptions().setMountValidationFailureHandler(false);
            // Mount the options
            routerFactory.setOptions(options);
            // Add an handler with operationId
            routerFactory.addHandlerByOperationId("listPets", routingContext -> {
                // Handle listPets operation
                routingContext.response().setStatusMessage("Called listPets").end();
            });
            // Add a failure handler to the same operationId
            routerFactory.addFailureHandlerByOperationId("listPets", routingContext -> {
                // This is the failure handler
                Throwable failure = routingContext.failure();
                if (failure instanceof ValidationException)
                    // Handle Validation Exception
                    routingContext.response().setStatusCode(400).setStatusMessage("ValidationException thrown! " + ((ValidationException) failure).type().name()).end();
            });
            // Add an handler with a combination of HttpMethod and path
            routerFactory.addHandler(HttpMethod.POST, "/pets", routingContext -> {
                // Extract request body and use it
                RequestParameters params = routingContext.get("parsedParameters");
                JsonObject pet = params.body().getJsonObject();
                routingContext.response().putHeader("content-type", "application/json; charset=utf-8").end(pet.encodePrettily());
            });
            // Add a security handler
            routerFactory.addSecurityHandler("api_key", routingContext -> {
                // Handle security here
                routingContext.next();
            });
            // Now you have to generate the router
            Router router = routerFactory.getRouter();
            // Now you can use your Router instance
            HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost"));
            server.requestHandler(router::accept).listen();
        } else {
            // Something went wrong during router factory initialization
            Throwable exception = openAPI3RouterFactoryAsyncResult.cause();
        }
    });
}
Also used : ValidationException(io.vertx.ext.web.api.validation.ValidationException) RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) JsonObject(io.vertx.core.json.JsonObject) Router(io.vertx.ext.web.Router) OpenAPI3RouterFactory(io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory) RequestParameters(io.vertx.ext.web.api.RequestParameters)

Aggregations

RouterFactoryOptions (io.vertx.ext.web.api.contract.RouterFactoryOptions)17 CountDownLatch (java.util.concurrent.CountDownLatch)13 Test (org.junit.Test)12 RequestParameters (io.vertx.ext.web.api.RequestParameters)5 ValidationException (io.vertx.ext.web.api.validation.ValidationException)4 MultiMap (io.vertx.core.MultiMap)3 HttpServerOptions (io.vertx.core.http.HttpServerOptions)3 JsonObject (io.vertx.core.json.JsonObject)3 Router (io.vertx.ext.web.Router)3 OpenAPI3RouterFactory (io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory)3 HttpClientOptions (io.vertx.core.http.HttpClientOptions)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 Handler (io.vertx.core.Handler)1 HttpMethod (io.vertx.core.http.HttpMethod)1 HttpServer (io.vertx.core.http.HttpServer)1 WebTestWithWebClientBase (io.vertx.ext.web.WebTestWithWebClientBase)1 RequestParameter (io.vertx.ext.web.api.RequestParameter)1 RouterFactoryException (io.vertx.ext.web.api.contract.RouterFactoryException)1 OpenAPI3RouterFactoryImpl (io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RouterFactoryImpl)1 BodyHandler (io.vertx.ext.web.handler.BodyHandler)1