Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Jackson to use SNAKE_CASE in Micronaut?

I have tried to configure jackson to accept JSON using SNAKE_CASE when using Micronaut, however, it doesn't recognize the property jackson.property-naming-strategy: SNAKE_CASE.

like image 956
Bruno Barin Avatar asked Nov 07 '18 17:11

Bruno Barin


2 Answers

From Micronaut 1.1.1

application.yml

jackson:
    property-naming-strategy: SNAKE_CASE

Before Micronaut 1.1.1

Micronaut constructs ObjectMapper using ObjectMapperFactory that does not set property naming strategy (at least in Micronaut 1.0 GA version, this may change in future releases). The configuration option you have mentioned in the question is not supported by the configuration class, so using it simply does nothing. However, you can replace ObjectMapperFactory class with your own custom implementation that constructs ObjectMapper in the default way + it sets property naming strategy. Consider following example:

package com.github.wololock.micronaut;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Replaces;
import io.micronaut.jackson.JacksonConfiguration;
import io.micronaut.jackson.ObjectMapperFactory;
import io.micronaut.runtime.Micronaut;

import javax.inject.Singleton;
import java.util.Optional;

public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class);
    }

    @Factory
    @Replaces(ObjectMapperFactory.class)
    static class CustomObjectMapperFactory extends ObjectMapperFactory {

        @Override
        @Singleton
        @Replaces(ObjectMapper.class)
        public ObjectMapper objectMapper(Optional<JacksonConfiguration> jacksonConfiguration, Optional<JsonFactory> jsonFactory) {
            final ObjectMapper mapper = super.objectMapper(jacksonConfiguration, jsonFactory);
            mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
            return mapper;
        }
    }
}

In this example I have added a static class CustomObjectMapperFactory to the main Application class and I have used @Replaces annotation to instruct Micronaut to use this factory class and objectMapper() method provided by this custom factory class. The ObjectMapper instance we return from this factory is based on the default factory method + it adds:

mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

to set expected property naming strategy.

And here is exemplary output I get back in the response after adding this custom factory class:

HTTP/1.1 200 OK
Date: Wed, 7 Nov 2018 19:15:10 GMT
connection: keep-alive
content-length: 38
content-type: application/json

{
    "first_name": "Joe",
    "last_name": "Doe"
}

By default (without this custom factory class) the response looked like this:

HTTP/1.1 200 OK
Date: Wed, 7 Nov 2018 19:04:14 GMT
connection: keep-alive
content-length: 36
content-type: application/json

{
    "firstName": "Joe",
    "lastName": "Doe"
}

UPDATE: Using BeanCreatedEventListener<ObjectMapper> instead

There is an alternative way to achieve the same effect that requires even less amount of lines of code. Credits goes to Micronaut Framework Twitter account :)

We can use BeanCreatedEventListener<T> that reacts to bean creation event and allows us extending the bean that just got created. In this case it means adding a class that implements BeanCreatedEventListener<ObjectMapper> and sets property naming strategy:

package com.github.wololock.micronaut;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import io.micronaut.context.event.BeanCreatedEvent;
import io.micronaut.context.event.BeanCreatedEventListener;
import io.micronaut.runtime.Micronaut;

import javax.inject.Singleton;

public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class);
    }

    @Singleton
    static class ObjectMapperBeanEventListener implements BeanCreatedEventListener<ObjectMapper> {

        @Override
        public ObjectMapper onCreated(BeanCreatedEvent<ObjectMapper> event) {
            final ObjectMapper mapper = event.getBean();
            mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
            return mapper;
        }
    }
}

Advantages of this solution:

  • less lines of code
  • one abstraction layer less (we don't have to bother with ObjectMapperFactory, we just care about configuring existing ObjectMapper bean).
like image 140
Szymon Stepniak Avatar answered Oct 07 '22 11:10

Szymon Stepniak


This is fixed as of Micronaut 1.1.1 in this issue: https://github.com/micronaut-projects/micronaut-core/issues/1599

Add this to your application.yml

jackson:
    property-naming-strategy: SNAKE_CASE

And you can test it with:

    @Test
    fun testJackson() {
        val applicationContext = ApplicationContext.run()

        assertThat(applicationContext.getBean(JacksonConfiguration::class.java).propertyNamingStrategy).isEqualTo(PropertyNamingStrategy.SNAKE_CASE)
    }
like image 28
hmatt1 Avatar answered Oct 07 '22 12:10

hmatt1