'How do we implement q Params in Helidon SE
I am new to Helidon SE and would like to know if there is a way to implement q params in REST service created via Helidon SE. Any help in this regard is truly appreciated.
Thanks, Gaurav
Solution 1:[1]
If you want to use and read params in the following way e.g.
http://localhost:8080/?q=test&k=test2
Then -in case of Helidon SE- do the following to get those parameters:
private void getParam(ServerRequest request, ServerResponse response) {
Map params = request.queryParams().toMap();
logger.info("params: " + params);
logger.info("q: " + params.get("q"));
logger.info("k: " + params.get("k"));
...
}
Obviously the getParam
method is configured for "/" path.
Solution 2:[2]
How do I retrieve query parameters in a Helidon SE application: Here is one example,
HelloService.java
public class HelloService implements Service {
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
HelloService(Config config) {
}
@Override
public void update(Rules rules) {
rules.get("/", this::getDefaultMessageHandler);
}
private void getDefaultMessageHandler(ServerRequest request, ServerResponse response) {
var params = request.queryParams().toMap();
System.out.println("name: " + params.get("name").get(0));
System.out.println("email: " + params.get("email").get(0));
sendResponse(response, params.get("name").get(0));
}
private void sendResponse(ServerResponse response, String name) {
var returnObject = JSON.createObjectBuilder().add("name", name).build();
response.send(returnObject);
}
}
Main
public class HelidonSeRestHelloWorldApplication {
public static void main(final String[] args) {
startServer();
}
private static Single<WebServer> startServer() {
var config = Config.create();
var server = WebServer.builder(createRouting(config)).config(config.get("server"))
.addMediaSupport(JsonpSupport.create()).build();
var webserver = server.start();
webserver.thenAccept(ws -> {
System.out.println("Web server started! http://localhost:" + ws.port() + "/hello");
ws.whenShutdown().thenRun(() -> System.out.println("Web server is down!"));
}).exceptionallyAccept(t -> {
System.out.println("Web startup failed: " + t.getMessage());
});
return webserver;
}
private static Routing createRouting(Config config) {
var helloService = new HelloService(config);
return Routing.builder()
.register("/hello", helloService).build();
}
}
application.properties
server.port=9080
Run the application and hit http://localhost:9080/hello?name=alpha&[email protected]
Console output:
name: alpha
email: [email protected]
++ How do I retrieve path parameters in a Helidon SE application:
Main
public class HelidonSeRestHelloWorldApplication {
...........................
...........................
private static Routing createRouting(Config config) {
var helloService = new HelloService(config);
return Routing.builder()
.register("/hello/{name}", helloService).build();
}
}
HelloService.java
public class HelloService implements Service {
............
...........
private void getDefaultMessageHandler(ServerRequest request, ServerResponse response) {
var name = request.path().absolute().param("name");
System.out.println(name);
sendResponse(response, name);
}
.................
.................
}
Run the application and hit: http://localhost:9080/hello/alpha
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | peter.nagy |
Solution 2 |