4

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

2 Answers 2

9

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Ok but I have to set up this in the application context? So far I did nothing except including Jackson jars in classpath.
I get this error in the spring config xml: Element 'mvc:annotation-driven' must have no character or element information item [children], because the type's content type is empty.
Got that error sorted out - was using older version of spring xml shcema - however solution does not work. breakpoint on NullSerializer never hit, though one on CustomerObjectMapper is hit. Dates still coming out as: "someDate":null instead of the desired "someDate":""
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.