Jackson is ignoring spring.jackson.properties in my spring boot application

后端 未结 6 1276
没有蜡笔的小新
没有蜡笔的小新 2021-02-19 06:01

Jackson is ignoring spring.jackson.property-naming-strategy=SNAKE_CASE. I am using springBootVersion 1.4.2.RELEASE. In my application.properties file, I have added

6条回答
  •  醉酒成梦
    2021-02-19 06:43

    MappingJackson2HttpMessageConverter class uses the default instance created by Jackson2ObjectMapperBuilder.json() method. In order to use application conext's ObjectMapper, you can register a custom WebMvcConfigurerAdapter.

    @Configuration
    public class WebMvcDefaultObjectMapperConfigurerAdapter extends WebMvcConfigurerAdapter {
    
        private ObjectMapper mapper;
    
        @Autowired
        public WebMvcDefaultObjectMapperConfigurerAdapter(ObjectMapper mapper) {
            // default mapper configured with spring.*
            this.mapper = mapper;
        }
    
        @Override
        public void extendMessageConverters(List> converters) {
            for (HttpMessageConverter httpConverter : converters) {
                if (httpConverter instanceof MappingJackson2HttpMessageConverter) {
                    // register the configured object mapper to HttpMessageconter
                    ((MappingJackson2HttpMessageConverter) httpConverter).setObjectMapper(mapper);
                }
            }
        }
    }
    

提交回复
热议问题