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("");
    }  
}

  

  

posted @ 2019-05-05 11:36  BINGJJFLY  阅读(1040)  评论(0编辑  收藏  举报