7.3.5 Customizing Jackson Settings

As mentioned previously, Jackson is used for message encoding to JSON. A default Jackson ObjectMapper is configured and used by Micronaut HTTP clients.

You can override the settings used to construct the ObjectMapper using the properties defined by the JacksonConfiguration class in application.yml.

For example, the following configuration enabled indented output for Jackson:

Example Jackson Configuration

  1. jackson:
  2. serialization:
  3. indentOutput: true

However, these settings apply globally and impact both how the HTTP server renders JSON and how JSON is sent from the HTTP client. Given that sometimes it useful to provide client specific Jackson settings which can be done with the @JacksonFeatures annotation on any client:

As an example, the following snippet is taken from Micronaut’s native Eureka client (which, of course, is built using Micronaut’s HTTP client):

Example of JacksonFeatures

  1. @Client(id = EurekaClient.SERVICE_ID, path = "/eureka", configuration = EurekaConfiguration.class)
  2. @JacksonFeatures(
  3. enabledSerializationFeatures = WRAP_ROOT_VALUE,
  4. disabledSerializationFeatures = WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED,
  5. enabledDeserializationFeatures = {UNWRAP_ROOT_VALUE, ACCEPT_SINGLE_VALUE_AS_ARRAY}
  6. )
  7. public interface EurekaClient {
  8. ...
  9. }

The Eureka serialization format for JSON uses the WRAP_ROOT_VALUE serialization feature of Jackson, hence it is enabled just for that client.

If the customization offered by JacksonFeatures is not enough, you can also write a BeanCreatedEventListener for the ObjectMapper and add whatever customizations you need.