Configuring ObjectMapper in Spring

前端 未结 12 1726
迷失自我
迷失自我 2020-11-22 10:46

my goal is to configure the objectMapper in the way that it only serialises element which are annotated with @JsonProperty.

In order to do

12条回答
  •  死守一世寂寞
    2020-11-22 11:19

    Above Spring 4, there is no need to configure MappingJacksonHttpMessageConverter if you only intend to configure ObjectMapper.

    (configure MappingJacksonHttpMessageConverter will cause you to lose other MessageConverter)

    You just need to do:

    public class MyObjectMapper extends ObjectMapper {
    
        private static final long serialVersionUID = 4219938065516862637L;
    
        public MyObjectMapper() {
            super();
            enable(SerializationFeature.INDENT_OUTPUT);
        }       
    }
    

    And in your Spring configuration, create this bean:

    @Bean 
    public MyObjectMapper myObjectMapper() {        
        return new MyObjectMapper();
    }
    

提交回复
热议问题