Java, Helidon SE get URL query string parameters example

Hello everyone, Today, we will show you how to create a simple REST API with Helidon SE 2.5.0 and Java 17 and how to get URL query string parameters.

Quick Overview:

  • Helidon SE is designed to be a microframework that fortifies the reactive programming model. Helidon SE features three core APIs to engender a microservice -- a web server, configuration, and security -- for building microservices-predicated applications. 
  • Helidon's web server is an asynchronous and reactive API that runs on top of Netty. The WebServer interface includes support for configuration, routing, error handling, and building metrics and health endpoints.
  • The Config loads and processes configuration properties(application.properties or application.yaml) in key/value format. 
  • The Security class provides support for authentication, sanction, and audit.
  • Query parameters are a defined set of parameters affixed to the end of a url. They are extensions of the URL that are used to avail define specific content or actions predicated on the data being passed.

Final Project Directory:


Maven [pom.xml]:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.helidon.applications</groupId>
<artifactId>helidon-se</artifactId>
<version>2.5.0</version>
<relativePath>../../../applications/se/pom.xml
</relativePath>
</parent>

<groupId>com.knf.dev.demo</groupId>
<artifactId>helidon-se-query-path-param-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helidon-se-query-path-param-example</name>
<description>Demo project for Helidon SE</description>

<properties>
<version.java>17</version.java>
<mainClass>com.knf.dev.demo.helidonseresthelloworld.
HelidonSeRestHelloWorldApplication</mainClass>
</properties>
<dependencies>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.media</groupId>
<artifactId>helidon-media-jsonp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>

<configuration>
<source>${version.java}</source>
<target>${version.java}</target>
<release>${version.java}</release>
<forceJavacCompilerUse>true
</forceJavacCompilerUse>
</configuration>
</plugin>
</plugins>
</build>
</project>

MyService.java:

package com.knf.dev.demo.helidonseresthelloworld;

import java.util.Collections;
import javax.json.Json;
import javax.json.JsonBuilderFactory;
import io.helidon.config.Config;
import io.helidon.webserver.Routing.Rules;
import io.helidon.webserver.ServerRequest;
import io.helidon.webserver.ServerResponse;
import io.helidon.webserver.Service;

public class MyService implements Service {

private static final JsonBuilderFactory JSON = Json.
createBuilderFactory(Collections.emptyMap());

MyService(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);
}
}

application.properties:

server.port=8080

Main.java:

package com.knf.dev.demo.helidonseresthelloworld;

import java.util.logging.Logger;
import io.helidon.common.reactive.Single;
import io.helidon.config.Config;
import io.helidon.media.jsonp.JsonpSupport;
import io.helidon.webserver.Routing;
import io.helidon.webserver.WebServer;

public class HelidonSeRestApplication {

private static final Logger LOGGER = Logger.
getLogger(HelidonSeRestApplication.class.getName());

public static void main(final String[] args) {
startServer();
}

private static Single<WebServer> startServer() {

// Load the default configuration using
// the create() method.
var config = Config.create();

var server = WebServer.builder(createRouting(config))
.config(config.get("server"))
.addMediaSupport(JsonpSupport.create())
.build();

// Start web server
var webserver = server.start();

webserver.thenAccept(ws -> {
LOGGER.info("Web server started! http://localhost:"
+ ws.port() + "/hello");
ws.whenShutdown().thenRun(() -> LOGGER.
info("Web server is down!"));
}).exceptionallyAccept(t -> {
LOGGER.severe("Web startup failed: " + t.getMessage());
});

return webserver;
}

private static Routing createRouting(Config config) {

var helloService = new MyService(config);

return Routing.builder()

.register("/hello", helloService).build();
}
}

Local Setup and Run the application:

Step1: Download or clone the source code to a local machine. - Click here


Step2: mvn clean install


Step3: Run the Main java application



Console output:

name: knowledgefactory 
email: knowledgefactory@knf.dev

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete