One of the (many) great features of Micronaut is the HTTP client. We use the
@Client annotation to inject a low-level HTTP client. Or we define a declarative HTTP client based on an interface, for which Micronaut will generate an implementation. The
@Client annotation supports the
configuration parameter to reference a configuration class with configuration properties for the HTTP client. The configuration class extends
HttpClientConfiguration to support for example the configuration of timeouts and connection pooling. We can add our own configuration properties as well and use them in our application.
Writing tests is always a good idea when developing an application. Micronaut makes it very easy to write tests. Using the
@Client annotation we can generate a client for our REST resources that uses HTTP. Starting up a Micronaut application is so fast we can run our actual application in our tests. And using dependency injection we can replace components from the production application with stubs in our tests.
Let’s show how we can use stub in our tests for an example application. In the example application we have controller
ConferenceController that returns
Conference objects. These objects are fetched from a simple data repository
ConferenceDataRepository. When we write a test we want to replace the
ConferenceDataRepository with a stub, so we can return the appropriate
Conference objects for our tests.
Spring REST Docs is a project to document a RESTful API using tests. The tests are used to invoke real REST calls on the application and to generate Asciidoctor markup snippets. We can use the generated snippets in an Asciidoctor document with documentation about our API. We can use Spring REST Docs to document a REST API we create using Micronaut.
Suppose we want our controller methods to return a JSON response when the HTTP Accept header is set to
application/json and XML when the Accept header is set to
application/xml. We can access the values of HTTP headers in our controller methods by adding an argument of type
HttpHeaders to our method definition and Micronaut will add all HTTP headers with their values as
HttpHeaders object when we run the application. In our method we can check the value of the Accept header and return a different value based on the header value.
When we add the
io.micronaut:management dependency to our Micronaut application we get, among other things, a
/health endpoint. We must enable it in our application configuration where we can also configure how much information is shown and if we want to secure the endpoint. Micronaut has some built-in health indicators, some of which are only available based on certain conditions. For example there is a disk space health indicator that will return a status of DOWN when the free disk space is less than a (configurable) threshold. If we would have one or more
DataSource beans for database access in our application context a health indicator is added as well to show if the database(s) are available or not.
We can also add our own health indicator that will show up in the
/health endpoint. We must write a class that implements the
HealthIndicator interface and add it to the application context. We could add some conditions to make sure the bean is loaded when needed. Micronaut also has the abstract
AbstractHealthIndicator class that can be used as base class for writing custom health indicators.
Micronaut has some built-in management endpoints to get information, a list of beans, health checks and more. To enable the endpoints we must add the dependency
io.micronaut:management to our application. Then we can add configuration properties to enable the different endpoints. The
/info endpoint gathers information from several sources with properties. If we want to add build information we must create a file
build-info.properties with information and Micronaut will automatically add the properties from the file to the
Micronaut uses Jackson to encode objects to JSON and decode JSON to objects. Micronaut adds a Jackson
ObjectMapper bean to the application context with all configuration to work properly. Jackson can by default populate an object with values from JSON as the class has a no argument constructor and the properties can be accessed. But if our class doesn’t have a no argument constructor we need to use the
@JsonProperty annotations to help Jackson. We can use these annotation on the constructor with arguments that is used to create an object.
But we can even make it work without the extra annotations, so our classes are easier to read and better reusable. We need to add the Jackson
ParameterNamesModule as module to the
ObjectMapper instance in our application. And we need to compile our sources with the
-parameter argument, so the argument names are preserved in the compiled code. Luckily the
-parameter option is already added to our Gradle build when we create a Micronaut application. All we have to do is to add the
ParameterNamesModule in our application.
Micronaut is reactive by nature and uses RxJava2 as implementation for the Reactive Streams API by default. RxJava2 is on the compile classpath by default, but we can easily use Project Reactor as implementation of the Reactive Streams API. This allows us to use the Reactor types
Flux. These types are also used by Spring’s Webflux framework and makes a transition from Webflux to Micronaut very easy.