1
1
package io .vertx .example .web .openapi3 ;
2
2
3
3
import io .vertx .core .AbstractVerticle ;
4
- import io .vertx .core .http . HttpMethod ;
4
+ import io .vertx .core .Future ;
5
5
import io .vertx .core .http .HttpServer ;
6
6
import io .vertx .core .http .HttpServerOptions ;
7
- import io .vertx .core .json .JsonObject ;
8
7
import io .vertx .core .logging .Logger ;
9
8
import io .vertx .core .logging .LoggerFactory ;
10
9
import io .vertx .example .util .Runner ;
10
+ import io .vertx .ext .web .Router ;
11
11
import io .vertx .ext .web .api .RequestParameter ;
12
12
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 ;
15
14
import io .vertx .ext .web .api .contract .openapi3 .OpenAPI3RouterFactory ;
16
15
import io .vertx .ext .web .api .validation .ValidationException ;
17
16
@@ -21,29 +20,31 @@ public class OpenAPI3Server extends AbstractVerticle {
21
20
HttpServer server ;
22
21
Logger logger = LoggerFactory .getLogger ("OpenAPI3RouterFactory" );
23
22
24
- public void start () {
23
+ public void start (Future future ) {
25
24
// Load the api spec. This operation is asynchronous
26
25
OpenAPI3RouterFactory .createRouterFactoryFromFile (this .vertx , getClass ().getResource ("/petstore.yaml" ).getFile (), openAPI3RouterFactoryAsyncResult -> {
27
26
if (openAPI3RouterFactoryAsyncResult .failed ()) {
28
27
// Something went wrong during router factory initialization
29
28
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 );
32
31
}
33
32
// Spec loaded with success
34
33
OpenAPI3RouterFactory routerFactory = openAPI3RouterFactoryAsyncResult .result ();
35
34
// Add an handler with operationId
36
35
routerFactory .addHandlerByOperationId ("listPets" , routingContext -> {
36
+ // Load the parsed parameters
37
37
RequestParameters params = routingContext .get ("parsedParameters" );
38
38
// Handle listPets operation
39
39
RequestParameter limitParameter = params .queryParameter (/* Parameter name */ "limit" );
40
40
if (limitParameter != null ) {
41
41
// limit parameter exists, use it!
42
42
Integer limit = limitParameter .getInteger ();
43
43
} 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!
45
46
}
46
- routingContext .response ().setStatusMessage ("Called listPets " ).end ();
47
+ routingContext .response ().setStatusMessage ("OK " ).end ();
47
48
});
48
49
// Add a failure handler
49
50
routerFactory .addFailureHandlerByOperationId ("listPets" , routingContext -> {
@@ -63,19 +64,27 @@ public void start() {
63
64
routingContext .next ();
64
65
});
65
66
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
68
71
69
72
// Now you have to generate the router
70
- Router router = routerFactory .getRouter ();
73
+ Router router = routerFactory .setOptions ( factoryOptions ). getRouter ();
71
74
72
75
// Now you can use your Router instance
73
76
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
+ });
75
86
});
76
87
77
- logger .info ("Server started!" );
78
-
79
88
}
80
89
81
90
public void stop () {
0 commit comments