spring mvc3 jackson输出null值处理

spring mvc3 jackson输出null值转为空串,找了很久终于找到办法

spring-mvc.xml中配置:


class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">


text/html;charset=UTF-8
application/json;charset=UTF-8










class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">









objectMapper类:

package com.tyrbl.zy.utils.spring;

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.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;

public class ObjectMappingCustomer extends ObjectMapper {

public ObjectMappingCustomer() {
super();
// // 允许单引号
// this.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// // 字段和值都加引号
// this.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
// // 数字也加引号
// this.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
// this.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, true);
// 空值处理为空串
this.getSerializerProvider().setNullValueSerializer(
new JsonSerializer() {

@Override
public void serialize(Object value, JsonGenerator jg,
SerializerProvider sp) throws IOException,
JsonProcessingException {
jg.writeString("");
}
});

}
}

你可能感兴趣的:(spring mvc3 jackson输出null值处理)