Building a HTTP Endpoint with Eclipse Vert.x
cescoffier
35.8K views
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Retrieving parameters
Let's now extend our code to retrieve a parameter passed in the url (query). You can retrieve query parameters using
the getParam
method:
Retrieving query parameters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package io.vertx.playground;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
public class HttpServerQueryExample {
public static void main(String... args) {
Vertx vertx = Vertx.vertx();
vertx.createHttpServer()
.requestHandler(req -> {
String name = req.getParam("name");
String message = "hello " + (name != null && ! name.trim().isEmpty() ? name : "world") + "!";
JsonObject json = new JsonObject()
.put("message", message);
req.response()
.putHeader("Content-Type", "application/json; charset=UTF8")
.end(json.encodePrettily());
})
.listen(8080);
}
}
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content