类 GsonHttpMessageConverter
- java.lang.Object
- org.springframework.http.converter.AbstractHttpMessageConverter<T>
- org.springframework.http.converter.AbstractGenericHttpMessageConverter<Object>
- org.springframework.http.converter.json.GsonHttpMessageConverter
- 所有已实现的接口:
GenericHttpMessageConverter<Object>,HttpMessageConverter<Object>
public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object>
Implementation ofHttpMessageConverterthat can read and write JSON using the Google Gson library'sGsonclass.This converter can be used to bind to typed beans or untyped
HashMaps. By default, it supportsapplication/jsonandapplication/*+jsonwithUTF-8character set.Tested against Gson 2.8; compatible with Gson 2.0 and higher.
- 从以下版本开始:
- 4.1
- 作者:
- Roy Clarkson
- 另请参阅:
setGson(com.google.gson.Gson),AbstractHttpMessageConverter.setSupportedMediaTypes(java.util.List<org.springframework.http.MediaType>)
字段概要
字段 修饰符和类型 字段 说明 static CharsetDEFAULT_CHARSET从类继承的字段 org.springframework.http.converter.AbstractHttpMessageConverter
logger
构造器概要
构造器 构造器 说明 GsonHttpMessageConverter()Construct a newGsonHttpMessageConverter.
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 com.google.gson.GsongetGson()Return the configuredGsoninstance for this converter.protected com.google.gson.reflect.TypeToken<?>getTypeToken(Type type)已过时。as of Spring Framework 4.3.8, in favor of signature-based resolutionObjectread(Type type, Class<?> contextClass, HttpInputMessage inputMessage)Read an object of the given type form the given input message, and returns it.protected ObjectreadInternal(Class<?> clazz, HttpInputMessage inputMessage)Abstract template method that reads the actual object.voidsetGson(com.google.gson.Gson gson)Set theGsoninstance to use.voidsetJsonPrefix(String jsonPrefix)Specify a custom prefix to use for JSON output.voidsetPrefixJson(boolean prefixJson)Indicate whether the JSON output by this view should be prefixed with ")]}', ".protected voidwriteInternal(Object o, Type type, HttpOutputMessage outputMessage)Abstract template method that writes the actual body.从类继承的方法 org.springframework.http.converter.AbstractGenericHttpMessageConverter
canRead, canWrite, supports, write, writeInternal
从类继承的方法 org.springframework.http.converter.AbstractHttpMessageConverter
addDefaultHeaders, canRead, canRead, canWrite, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 org.springframework.http.converter.HttpMessageConverter
canRead, canWrite, getSupportedMediaTypes, read, write
字段详细资料
DEFAULT_CHARSET
public static final Charset DEFAULT_CHARSET
构造器详细资料
GsonHttpMessageConverter
public GsonHttpMessageConverter()
Construct a newGsonHttpMessageConverter.
方法详细资料
setGson
public void setGson(com.google.gson.Gson gson)
Set theGsoninstance to use. If not set, a defaultGsoninstance will be used.Setting a custom-configured
Gsonis one way to take further control of the JSON serialization process.
getGson
public com.google.gson.Gson getGson()
Return the configuredGsoninstance for this converter.
setJsonPrefix
public void setJsonPrefix(String jsonPrefix)
Specify a custom prefix to use for JSON output. Default is none.
setPrefixJson
public void setPrefixJson(boolean prefixJson)
Indicate whether the JSON output by this view should be prefixed with ")]}', ". Default isfalse.Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. The prefix renders the string syntactically invalid as a script so that it cannot be hijacked. This prefix should be stripped before parsing the string as JSON.
read
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
从接口复制的说明:GenericHttpMessageConverterRead an object of the given type form the given input message, and returns it.- 参数:
type- the (potentially generic) type of object to return. This type must have previously been passed to thecanReadmethod of this interface, which must have returnedtrue.contextClass- a context class for the target type, for example a class in which the target type appears in a method signature (can benull)inputMessage- the HTTP input message to read from- 返回:
- the converted object
- 抛出:
IOException- in case of I/O errorsHttpMessageNotReadableException- in case of conversion errors
readInternal
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
从类复制的说明:AbstractHttpMessageConverterAbstract template method that reads the actual object. Invoked fromAbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage).- 指定者:
readInternal在类中AbstractHttpMessageConverter<Object>- 参数:
clazz- the type of object to returninputMessage- the HTTP input message to read from- 返回:
- the converted object
- 抛出:
IOException- in case of I/O errorsHttpMessageNotReadableException- in case of conversion errors
getTypeToken
@Deprecated protected com.google.gson.reflect.TypeToken<?> getTypeToken(Type type)
已过时。as of Spring Framework 4.3.8, in favor of signature-based resolutionReturn the GsonTypeTokenfor the specified type.The default implementation returns
TypeToken.get(type), but this can be overridden in subclasses to allow for custom generic collection handling. For instance:protected TypeToken> getTypeToken(Type type) { if (type instanceof Class && List.class.isAssignableFrom((Class>) type)) { return new TypeToken>() {}; } else { return super.getTypeToken(type); } } - 参数:
type- the type for which to return the TypeToken- 返回:
- the type token
writeInternal
protected void writeInternal(Object o, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
从类复制的说明:AbstractGenericHttpMessageConverterAbstract template method that writes the actual body. Invoked fromAbstractGenericHttpMessageConverter.write(T, java.lang.reflect.Type, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage).- 指定者:
writeInternal在类中AbstractGenericHttpMessageConverter<Object>- 参数:
o- the object to write to the output messagetype- the type of object to write (may benull)outputMessage- the HTTP output message to write to- 抛出:
IOException- in case of I/O errorsHttpMessageNotWritableException- in case of conversion errors