Null values as empty strings when using @ResponseBody annotation

前端 未结 2 1128
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 05:49

Is there a way when using @ResponseBody annotation to have null values mapped to empty strings?

相关标签:
2条回答
  • 2020-12-17 06:07

    You will have to write a custom Jackson Serializer - a good example is here - http://wiki.fasterxml.com/JacksonHowToCustomSerializers (there is a specific example of how to convert null values to empty Strings that you can use)

    Here are all the steps(for Jackson < 2.0):

    Write your custom null Serializer:

    import java.io.IOException;
    
    import org.codehaus.jackson.JsonGenerator;
    import org.codehaus.jackson.JsonProcessingException;
    import org.codehaus.jackson.map.JsonSerializer;
    import org.codehaus.jackson.map.SerializerProvider;
    
    public class NullSerializer extends JsonSerializer<Object> {
    
        @Override
        public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
            jgen.writeString("");
        }
    }
    

    Register this with Jackson Objectmapper:

    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.ser.StdSerializerProvider;
    
    
    public class CustomObjectMapper extends ObjectMapper{
        public CustomObjectMapper(){
            StdSerializerProvider sp = new StdSerializerProvider();
            sp.setNullValueSerializer(new NullSerializer());
            this.setSerializerProvider(sp);
        }
    
    }
    

    Register this objectmapper with Spring MVC:

    <mvc:annotation-driven> 
       <mvc:message-converters register-defaults="true">
           <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
               <property name="objectMapper">
                   <bean class="CustomObjectMapper"/>
               </property>
           </bean>
       </mvc:message-converters>
    </mvc:annotation-driven>
    
    0 讨论(0)
  • 2020-12-17 06:09

    I have also faced the same problem in my project and I have therefore quickly come up with a solution for the same. This post will surely help all those who have been struggling with the same issue.

    Step 1:- Create your Custom Null Handler Serializer.

    public class NullSerializer extends StdSerializer<Object> {
    
        public NullSerializer(Class<Object> t) {
           super(t);
        }
    
        public NullSerializer() {
          this(null);
        }
    
        @Override
        public void serialize(Object o, com.fasterxml.jackson.core.JsonGenerator jsonGenerator, com.fasterxml.jackson.databind.SerializerProvider serializerProvider) throws IOException {
           jsonGenerator.writeString("");
        }
       }
    

    Step 2:- Create a bean of MappingJackson2HttpMessageConverter.

    @Bean
    @Primary
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        mapper.getSerializerProvider().setNullValueSerializer(new NullSerializer());
        jsonConverter.setObjectMapper(mapper);
        return jsonConverter;
    
    } 
    

    Thank you for taking some time out to read this post. I hope that this was able to resolve your queries to some extent.

    0 讨论(0)
提交回复
热议问题