Skip to content

Commit 494b14f

Browse files
authored
Merge pull request vert-x3#263 from vert-x3/3.5.1-Web-OpenAPI-Update
Updated openapi example for 3.5.1 release
2 parents 250b6e7 + 1625079 commit 494b14f

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

web-examples/README.adoc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,11 @@ Plus how to interact with secured resources using the user object directly.
416416

417417
The link:src/main/java/io/vertx/example/web/oauth2/Server.java[OAuth2 example]
418418

419-
== OpenAPI 3 server
419+
== HTTP Request Validation and OpenAPI 3 Router Factory
420420

421-
The OpenAPI 3 server the creation of a Vert.x Web router handler that performs web request/response validation
422-
based on an OpenAPI 3 yaml schema for of the Swagger Petstore application.
421+
The link:src/main/java/io/vertx/example/web/validation/ValidationExampleServer.java[ValidationExampleServer] is an
422+
example of various usages of validation capabilities included in
423+
link:http://vertx.io/docs/#web[Vert.x Web API Contract package]
423424

424-
The link:src/main/java/io/vertx/example/web/openapi3/OpenAPI3Server.java[OpenAPI 3 example]
425+
The link:src/main/java/io/vertx/example/web/openapi3/OpenAPI3Server.java[OpenAPI3Server] is an example of
426+
OpenAPI3RouterFactory, the interface to build your design driven router based on your OpenAPI specification.

web-examples/src/main/java/io/vertx/example/web/openapi3/OpenAPI3Server.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package io.vertx.example.web.openapi3;
22

33
import io.vertx.core.AbstractVerticle;
4-
import io.vertx.core.http.HttpMethod;
4+
import io.vertx.core.Future;
55
import io.vertx.core.http.HttpServer;
66
import io.vertx.core.http.HttpServerOptions;
7-
import io.vertx.core.json.JsonObject;
87
import io.vertx.core.logging.Logger;
98
import io.vertx.core.logging.LoggerFactory;
109
import io.vertx.example.util.Runner;
10+
import io.vertx.ext.web.Router;
1111
import io.vertx.ext.web.api.RequestParameter;
1212
import io.vertx.ext.web.api.RequestParameters;
13-
import io.vertx.ext.web.RoutingContext;
14-
import io.vertx.ext.web.Router;
13+
import io.vertx.ext.web.api.contract.DesignDrivenRouterFactoryOptions;
1514
import io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory;
1615
import io.vertx.ext.web.api.validation.ValidationException;
1716

@@ -21,29 +20,31 @@ public class OpenAPI3Server extends AbstractVerticle {
2120
HttpServer server;
2221
Logger logger = LoggerFactory.getLogger("OpenAPI3RouterFactory");
2322

24-
public void start() {
23+
public void start(Future future) {
2524
// Load the api spec. This operation is asynchronous
2625
OpenAPI3RouterFactory.createRouterFactoryFromFile(this.vertx, getClass().getResource("/petstore.yaml").getFile(), openAPI3RouterFactoryAsyncResult -> {
2726
if (openAPI3RouterFactoryAsyncResult.failed()) {
2827
// Something went wrong during router factory initialization
2928
Throwable exception = openAPI3RouterFactoryAsyncResult.cause();
30-
logger.error("oops!", exception);
31-
this.stop();
29+
logger.error("oops, something went wrong during factory initialization", exception);
30+
future.fail(exception);
3231
}
3332
// Spec loaded with success
3433
OpenAPI3RouterFactory routerFactory = openAPI3RouterFactoryAsyncResult.result();
3534
// Add an handler with operationId
3635
routerFactory.addHandlerByOperationId("listPets", routingContext -> {
36+
// Load the parsed parameters
3737
RequestParameters params = routingContext.get("parsedParameters");
3838
// Handle listPets operation
3939
RequestParameter limitParameter = params.queryParameter(/* Parameter name */ "limit");
4040
if (limitParameter != null) {
4141
// limit parameter exists, use it!
4242
Integer limit = limitParameter.getInteger();
4343
} else {
44-
// limit parameter doesn't exist (it's not required). If it's required you don't have to check if it's null!
44+
// limit parameter doesn't exist (it's not required).
45+
// If it's required you don't have to check if it's null!
4546
}
46-
routingContext.response().setStatusMessage("Called listPets").end();
47+
routingContext.response().setStatusMessage("OK").end();
4748
});
4849
// Add a failure handler
4950
routerFactory.addFailureHandlerByOperationId("listPets", routingContext -> {
@@ -63,19 +64,27 @@ public void start() {
6364
routingContext.next();
6465
});
6566

66-
// Before router creation you can enable or disable mounting of a default failure handler for ValidationException
67-
routerFactory.enableValidationFailureHandler(false);
67+
// Before router creation you can enable/disable various router factory behaviours
68+
DesignDrivenRouterFactoryOptions factoryOptions = new DesignDrivenRouterFactoryOptions()
69+
.setMountValidationFailureHandler(false) // Disable mounting of dedicated validation failure handler
70+
.setMountResponseContentTypeHandler(true); // Mount ResponseContentTypeHandler automatically
6871

6972
// Now you have to generate the router
70-
Router router = routerFactory.getRouter();
73+
Router router = routerFactory.setOptions(factoryOptions).getRouter();
7174

7275
// Now you can use your Router instance
7376
server = vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost"));
74-
server.requestHandler(router::accept).listen();
77+
server.requestHandler(router::accept).listen((ar) -> {
78+
if (ar.succeeded()) {
79+
logger.info("Server started on port " + ar.result().actualPort());
80+
future.complete();
81+
} else {
82+
logger.error("oops, something went wrong during server initialization", ar.cause());
83+
future.fail(ar.cause());
84+
}
85+
});
7586
});
7687

77-
logger.info("Server started!");
78-
7988
}
8089

8190
public void stop() {

0 commit comments

Comments
 (0)