Class GsonHttpMessageConverter
- java.lang.Object
- org.springframework.http.converter.AbstractHttpMessageConverter<T>
- org.springframework.http.converter.AbstractGenericHttpMessageConverter<Object>
- org.springframework.http.converter.json.GsonHttpMessageConverter
- All Implemented Interfaces:
GenericHttpMessageConverter<Object>
,HttpMessageConverter<Object>
public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object>
Implementation ofHttpMessageConverter
that can read and write JSON using the Google Gson library'sGson
class.This converter can be used to bind to typed beans or untyped
HashMap
s. By default, it supportsapplication/json
andapplication/*+json
withUTF-8
character set.Tested against Gson 2.8; compatible with Gson 2.0 and higher.
- Since:
- 4.1
- Author:
- Roy Clarkson
- See Also:
setGson(com.google.gson.Gson)
,AbstractHttpMessageConverter.setSupportedMediaTypes(java.util.List<org.springframework.http.MediaType>)
Field Summary
Fields Modifier and Type Field Description static Charset
DEFAULT_CHARSET
Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
logger
Constructor Summary
Constructors Constructor Description GsonHttpMessageConverter()
Construct a newGsonHttpMessageConverter
.
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description com.google.gson.Gson
getGson()
Return the configuredGson
instance for this converter.protected com.google.gson.reflect.TypeToken<?>
getTypeToken(Type type)
Deprecated.as of Spring Framework 4.3.8, in favor of signature-based resolutionObject
read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
Read an object of the given type form the given input message, and returns it.protected Object
readInternal(Class<?> clazz, HttpInputMessage inputMessage)
Abstract template method that reads the actual object.void
setGson(com.google.gson.Gson gson)
Set theGson
instance to use.void
setJsonPrefix(String jsonPrefix)
Specify a custom prefix to use for JSON output.void
setPrefixJson(boolean prefixJson)
Indicate whether the JSON output by this view should be prefixed with ")]}', ".protected void
writeInternal(Object o, Type type, HttpOutputMessage outputMessage)
Abstract template method that writes the actual body.Methods inherited from class org.springframework.http.converter.AbstractGenericHttpMessageConverter
canRead, canWrite, supports, write, writeInternal
Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
addDefaultHeaders, canRead, canRead, canWrite, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.http.converter.HttpMessageConverter
canRead, canWrite, getSupportedMediaTypes, read, write
Field Detail
DEFAULT_CHARSET
public static final Charset DEFAULT_CHARSET
Constructor Detail
GsonHttpMessageConverter
public GsonHttpMessageConverter()
Construct a newGsonHttpMessageConverter
.
Method Detail
setGson
public void setGson(com.google.gson.Gson gson)
Set theGson
instance to use. If not set, a defaultGson
instance will be used.Setting a custom-configured
Gson
is one way to take further control of the JSON serialization process.
getGson
public com.google.gson.Gson getGson()
Return the configuredGson
instance for this converter.
setJsonPrefix
public void setJsonPrefix(String jsonPrefix)
Specify a custom prefix to use for JSON output. Default is none.- See Also:
setPrefixJson(boolean)
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.
- See Also:
setJsonPrefix(java.lang.String)
read
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
Description copied from interface:GenericHttpMessageConverter
Read an object of the given type form the given input message, and returns it.- Parameters:
type
- the (potentially generic) type of object to return. This type must have previously been passed to thecanRead
method 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- Returns:
- the converted object
- Throws:
IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errors
readInternal
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
Description copied from class:AbstractHttpMessageConverter
Abstract template method that reads the actual object. Invoked fromAbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.- Specified by:
readInternal
in classAbstractHttpMessageConverter<Object>
- Parameters:
clazz
- the type of object to returninputMessage
- the HTTP input message to read from- Returns:
- the converted object
- Throws:
IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errors
getTypeToken
@Deprecated protected com.google.gson.reflect.TypeToken<?> getTypeToken(Type type)
Deprecated.as of Spring Framework 4.3.8, in favor of signature-based resolutionReturn the GsonTypeToken
for 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); } } - Parameters:
type
- the type for which to return the TypeToken- Returns:
- the type token
writeInternal
protected void writeInternal(Object o, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
Description copied from class:AbstractGenericHttpMessageConverter
Abstract template method that writes the actual body. Invoked fromAbstractGenericHttpMessageConverter.write(T, java.lang.reflect.Type, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.- Specified by:
writeInternal
in classAbstractGenericHttpMessageConverter<Object>
- Parameters:
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- Throws:
IOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errors