一、json requestbody 輸要用單引號(jackson默認是雙引號,單引號直接報錯)java
二、responsebody數字輸出要加引號,字段名要加引號。web
三、null值爲空串spring
Xml代碼json
因而查閱官方文檔,終於找到方法,有點小搓,直接在Converter對象上注入一個自定義類ObjectMappingCustomer:mvc
<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代碼app
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(""); } }); } }