springmvc json串的null值替换为空值

springmvc配置文件

<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
	<property name="supportedMediaTypes">
		<list>
			<value>text/html;charset=UTF-8</value>
			<value>application/json;charset=UTF-8</value>
		</list>
	</property>
    <property name="objectMapper">  
        <bean class="com.fasterxml.jackson.databind.ObjectMapper">  
            <property name="serializerProvider">  
                <bean class="com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.Impl">  
                    <property name="nullValueSerializer">  
                        <bean class="com.xxx.xxx.NullValueSerializer"></bean>  
                    </property>  
                </bean>  
            </property>  
        </bean>  
    </property>    
</bean>

NullValueSerializer

public class NullValueSerializer extends JsonSerializer<Object> {  
    @Override  
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)  
            throws IOException, JsonProcessingException {  
        jgen.writeString("");
    }  
}

  

  

上一篇:Java-Class-C:org.springframework.http.converter.StringHttpMessageConverter


下一篇:java中更新文件时,指定编码格式