在學習Spring的時候看到可使用@RequestBody 和@ResponseBody註解來是的Spring自動將http 其中的 body(json格式)部分和java內部的類進行轉換。同時因爲Google Gson的強大通常開發的時候會用的比較多,可是因爲Spring內部默認使用的json的message Converter 並非gson,因此這裏須要配置一下才能使其生效;html
Spring其實已經在實現了對gson的支持,可是若是想在項目中使用,還須要經過配置一下才能夠:java
配置文件以下:jquery
<!--<mvc:annotation-driven/>-->
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.GsonHttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven>
其中org.springframework.http.converter.json.GsonHttpMessageConverter
是Spring已經實現了的,在spring的包中能找到這個類。git
注:若是想要某中類型的數據能被Message Converter轉換,須要本身實現AbstractGenericHttpMessageConverter
這個接口,而後在經過配置文件將這個類加載進你的項目中(好比說alibaba:fastjson,在它包中也實現了度spring message converter 的支持)。github
備註: 網上有不少實現本身的Message Converter的教程,若是後續須要能夠搜一下,實現起來有兩個步驟:web
須要本身實現AbstractGenericHttpMessageConverter
這個接口;spring
經過xml或者其餘方法配置一下json
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.GsonHttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> #在pom下加入依賴 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> </dependency>
一下貼點源碼:api
package org.springframework.http.converter.json; import com.google.gson.Gson; import com.google.gson.JsonIOException; import com.google.gson.JsonParseException; import com.google.gson.reflect.TypeToken; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.lang.reflect.Type; import java.nio.charset.Charset; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.AbstractGenericHttpMessageConverter; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.util.Assert; public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private Gson gson = new Gson(); private String jsonPrefix; public GsonHttpMessageConverter() { super(new MediaType[]{MediaType.APPLICATION_JSON, new MediaType("application", "*+json")}); this.setDefaultCharset(DEFAULT_CHARSET); } public void setGson(Gson gson) { Assert.notNull(gson, "'gson' is required"); this.gson = gson; } public Gson getGson() { return this.gson; } public void setJsonPrefix(String jsonPrefix) { this.jsonPrefix = jsonPrefix; } public void setPrefixJson(boolean prefixJson) { this.jsonPrefix = prefixJson?")]}', ":null; } public boolean canRead(Class<?> clazz, MediaType mediaType) { return this.canRead(mediaType); } public boolean canWrite(Class<?> clazz, MediaType mediaType) { return this.canWrite(mediaType); } protected boolean supports(Class<?> clazz) { throw new UnsupportedOperationException(); } protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { TypeToken<?> token = this.getTypeToken(clazz); return this.readTypeToken(token, inputMessage); } public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { TypeToken<?> token = this.getTypeToken(