1、json requestbody 输要用单引号(jackson默认是双引号,单引号直接报错)
2、responsebody数字输出要加引号,字段名要加引号。
3、null值为空串
Xml代码
于是查阅官方文档,终于找到方法,有点小搓,直接在Converter对象上注入一个自定义类ObjectMappingCustomer:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="objectMapper">
<bean class="com.wuliaolll.util.ObjectMappingCustomer"></bean>
</property>
</bean>
</list>
</property>
</bean>
Java代码
package com.wuliaolll.util;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
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<Object>()
{
@Override
public void serialize(
Object value,
JsonGenerator jg,
SerializerProvider sp) throws IOException, JsonProcessingException
{
jg.writeString("");
}
});
}
}