forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationExampleServer.java
More file actions
101 lines (86 loc) · 3.53 KB
/
ValidationExampleServer.java
File metadata and controls
101 lines (86 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package io.vertx.example.web.validation;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonObject;
import io.vertx.example.util.Runner;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.validation.BadRequestException;
import io.vertx.ext.web.validation.RequestParameters;
import io.vertx.ext.web.validation.ValidationHandler;
import io.vertx.json.schema.SchemaParser;
import io.vertx.json.schema.SchemaRouter;
import io.vertx.json.schema.SchemaRouterOptions;
import static io.vertx.ext.web.validation.builder.Parameters.*;
import static io.vertx.ext.web.validation.builder.Bodies.*;
import static io.vertx.json.schema.draft7.dsl.Schemas.*;
/**
* @author <a href="https://slinkydeveloper.com">Francesco Guardiani</a>
*/
public class ValidationExampleServer extends AbstractVerticle {
@Override
public void start() throws Exception {
// The schema parser is required to create new schemas
SchemaParser parser = SchemaParser.createDraft7SchemaParser(
SchemaRouter.create(vertx, new SchemaRouterOptions())
);
Router router = Router.router(vertx);
// If you want to validate bodies, don't miss that handler!
router.route().handler(BodyHandler.create());
// Create Validation Handler with some stuff
ValidationHandler validationHandler = ValidationHandler.builder(parser)
.queryParameter(param("parameterName", intSchema()))
.pathParameter(param("pathParam", numberSchema()))
.body(
formUrlEncoded(
objectSchema()
.property("r", intSchema())
.property("g", intSchema())
.property("b", intSchema())
)
)
.build();
router.post("/hello/:pathParam")
// Mount validation handler
.handler(validationHandler)
//Mount your handler
.handler((routingContext) -> {
// To consume parameters you can get it in the "classic way" of Vert.x Web
// or you can use the RequestParameters that contains parameters already parsed (and maybe transformed)
// Get RequestParameters container
RequestParameters params = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
// Get parameters
Integer parameterName = params.queryParameter("parameterName").getInteger();
Float pathParam = params.pathParameter("pathParam").getFloat();
// Get body
JsonObject rgbObject = params.body().getJsonObject();
// Do awesome things with your parameters!
});
// A very basic example of JSON body validation
router.post("/jsonUploader")
.handler(ValidationHandler.builder(parser)
.body(json(
objectSchema()
.additionalProperties(stringSchema())
))
.build()
)
.handler((routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
JsonObject body = params.body().getJsonObject();
}));
// Custom handler for the ValidationException
router.errorHandler(400, routingContext -> {
Throwable failure = routingContext.failure();
if (failure instanceof BadRequestException) {
// Something went wrong during validation!
String validationErrorMessage = failure.getMessage();
routingContext.response().setStatusCode(400).end();
}
});
vertx.createHttpServer().requestHandler(router).listen();
}
// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Runner.runExample(ValidationExampleServer.class);
}
}