Object Computing, Inc. has introduced Micronaut Servlet, a new Micronaut project that implements a Micronaut HTTP server backed onto the Servlet API that allows popular Servlet containers to run as servers.
Micronaut Servlet provides an alternative for developers who are familiar with traditional servlet containers and have a significant investment in the servlet ecosystem. In particular, these developers generally fall into one of three categories: [1] those want to use Micronaut, but the target deployment environment is based on servlets; [2] those who prefer the thread-per-connection model of the Servlet API over the Event Loop model provided by the default Netty-based Micronaut HTTP server; and [3] those who have existing servlets and/or servlet filters that they wish to combine with Micronaut.
Originally known as Project Particle, Micronaut is a full-stack JVM-based framework for creating microservice-based, cloud-native and serverless applications that can be written in Java, Groovy, and Kotlin. Micronaut was introduced in March 2018 by Graeme Rocher, principal software engineer and Grails and Micronaut product lead at OCI, and was subsequently open-sourced in May 2018.
The specific non-Netty features of Micronaut's HTTP built-in server works with the supported servlet containers, namely Tomcat, Jetty and Undertow. Micronaut Servlet includes several extensions to simplify working with the Servlet API. This includes the ability to: receive the traditional Servlet API interfaces, HTTPServletRequest
and HttpServletResponse
, as method parameters; utilize Micronaut interfaces, Readable
and Writable
, to simplify servlet read/write operations; and support for multipart forms.
In traditional Servlet API development, it is required to override the methods, doGet()
, doPost()
, etc., using the HTTPServletRequest
and HTTPServletResponse
interfaces for handling HTTP verbs within the lifecycle of the servlet application. These interfaces may now be passed into user-defined methods as shown here:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Get("/hello")
void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
response.setStatus(HttpStatus.ACCEPTED.getCode());
try (final PrintWriter writer = response.getWriter()) {
writer.append("Hello ").append(request.getParameter("name"));
writer.flush();
}
}
The process()
method is decorated with the @Get
annotation to define the REST endpoint, /hello
, and accepts the two HTTP interfaces to build the server response. This provides a more elegant solution to the Servlet API methods.
The new Readable
and Writable
interfaces in Micronaut Servlet were provided to simplify the operations of reading from the servlet request and writing to the servlet response:
import io.micronaut.core.io.Readable;
import io.micronaut.core.io.Writable;
@Post(value = "/writable", processes = "text/plain")
Writable readAndWrite(@Body Readable readable) throws IOException {
return out -> {
try (BufferedReader reader = new BufferedReader(readable.asReader())) {
out.append("Hello ").append(reader.readLine());
}
};
}
The Micronaut annotation, @Part
, may be applied to method arguments to indicate they are bound to a specific part of a multipart/form-data POST
request. Consider the following method:
@Post(value = "/multipart", consumes = MediaType.MULTIPART_FORM_DATA, produces = "text/plain")
String multipart(
String attribute,
@Part("one") Person person,
@Part("two") String text,
@Part("three") byte[] bytes,
@Part("four") javax.servlet.http.Part raw,
@Part("five") CompletedPart part) {
return "Ok";
}
The first parameter, attribute
, within the multipart()
method represents a list of attributes with parameter names that match an attribute. The remaining parameters, decorated with the @Part
annotation, are declared as:
- content type
application/json
that may be bound to a POJO - type
String
- type
byte
javax.servlet.http.Part
, an Servlet API interfaceCompletedPart
, a Micronaut interface that represents a completed part of a multipart request.
Multipart handling is disabled by default, but may be enabled by following the configuration properties.
GraalVM support is also available for Micronaut Servlet, but only Tomcat and Jetty may take advantage of GraalVM at this time. Applications using Undertow will not compile with GraalVM.
Micronaut Servlet joins a host of Micronaut projects such as Micronaut AWS, Micronaut GCP, Micronaut Data, Micronaut for Spring and Micronaut Test.