This guide shows how reuse Helidon SE Service in your Helidon MP application.
What You Need
For this 10 minute tutorial, you will need the following:
Helidon requires Java 21+ (25+ recommended). |
|
Helidon requires Maven 3.8+. |
|
If you want to build and run Docker containers. |
|
If you want to deploy to Kubernetes, you need |
java -version
mvn --version
docker --version
kubectl version
# On Mac
export JAVA_HOME=`/usr/libexec/java_home -v 21`
# On Linux
# Use the appropriate path to your JDK
export JAVA_HOME=/usr/lib/jvm/jdk-21
Helidon MP supports WebServer routing which brings possibility for reusing
io.helidon.webserver.HttpService implementations in Helidon MP. Such feature can be quite useful for common
solutions for filtering, auditing, logging or augmenting REST endpoints in hybrid Helidon SE/MP environment.
Let’s define simple Helidon SE Service for adding special header to every REST response:
public class CoolingService implements HttpService, Handler {
public static final HeaderName COOL_HEADER_NAME = HeaderNames.create("Cool-Header");
public static final String COOLING_VALUE = "This is way cooler response than ";
@Override
public void routing(HttpRules rules) {
rules.any(this);
}
@Override
public void handle(ServerRequest req, ServerResponse res) {
res.headers().add(COOL_HEADER_NAME, COOLING_VALUE);
res.next();
}
}
It’s easy to use it with Helidon SE:
WebServer.builder()
.routing(it -> it
.register("/cool", new CoolingService())) // (1)
.config(config)
.mediaContext(it -> it
.addMediaSupport(JsonpSupport.create()))
.build()
.start();
-
register service with routing path
And not much harder to use it with Helidon MP:
@ApplicationScoped
public class MyBean {
@Produces
@ApplicationScoped
@RoutingPath("/cool")
public HttpService coolService() {
return new CoolingService();
}
}
You can leverage annotations:
-
@RoutingPath - path of the WebServer service
-
@RoutingName - select routing when serving requests on multiple ports