public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> implements GenericHttpMessageConverter<Object>
HttpMessageConverter
that can read and write JSON using the
Google Gson library's
Gson
class.
This converter can be used to bind to typed beans or untyped HashMap
s.
By default, it supports application/json
and application/*+json
.
Tested against Gson 2.3; compatible with Gson 2.0 and higher.
setGson(com.google.gson.Gson)
,
AbstractHttpMessageConverter.setSupportedMediaTypes(java.util.List<org.springframework.http.MediaType>)
Modifier and Type | Field and Description |
---|---|
static Charset |
DEFAULT_CHARSET |
logger
Constructor and Description |
---|
GsonHttpMessageConverter()
Construct a new
GsonHttpMessageConverter . |
Modifier and Type | Method and Description |
---|---|
boolean |
canRead(Class<?> clazz,
MediaType mediaType)
This implementation checks if the given class is supported,
and if the supported media types
include the given media type.
|
boolean |
canWrite(Class<?> clazz,
MediaType mediaType)
This implementation checks if the given class is supported,
and if the supported media types
include the given media type.
|
com.google.gson.Gson |
getGson()
Return the configured
Gson instance for this converter. |
protected com.google.gson.reflect.TypeToken<?> |
getTypeToken(Type type)
Return the Gson
TypeToken for the specified type. |
Object |
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 the
Gson 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 boolean |
supports(Class<?> clazz)
Indicates whether the given class is supported by this converter.
|
protected void |
writeInternal(Object o,
Type type,
HttpOutputMessage outputMessage)
Abstract template method that writes the actual body.
|
canRead, canWrite, write, writeInternal
addDefaultHeaders, canRead, canWrite, getContentLength, getDefaultContentType, getSupportedMediaTypes, read, setSupportedMediaTypes, write
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
canRead, canWrite, write
getSupportedMediaTypes, read, write
public static final Charset DEFAULT_CHARSET
public GsonHttpMessageConverter()
GsonHttpMessageConverter
.public void setGson(com.google.gson.Gson gson)
Gson
instance to use.
If not set, a default Gson
instance is used.
Setting a custom-configured Gson
is one way to take further
control of the JSON serialization process.
public com.google.gson.Gson getGson()
Gson
instance for this converter.public void setJsonPrefix(String jsonPrefix)
setPrefixJson(boolean)
public void setPrefixJson(boolean prefixJson)
false
.
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.
setJsonPrefix(java.lang.String)
public boolean canRead(Class<?> clazz, MediaType mediaType)
AbstractHttpMessageConverter
canRead
in interface HttpMessageConverter<Object>
canRead
in class AbstractHttpMessageConverter<Object>
clazz
- the class to test for readabilitymediaType
- the media type to read, can be null
if not specified.
Typically the value of a Content-Type
header.true
if readable; false
otherwisepublic boolean canWrite(Class<?> clazz, MediaType mediaType)
AbstractHttpMessageConverter
canWrite
in interface HttpMessageConverter<Object>
canWrite
in class AbstractHttpMessageConverter<Object>
clazz
- the class to test for writabilitymediaType
- the media type to write, can be null
if not specified.
Typically the value of an Accept
header.true
if writable; false
otherwiseprotected boolean supports(Class<?> clazz)
AbstractHttpMessageConverter
supports
in class AbstractHttpMessageConverter<Object>
clazz
- the class to test for supporttrue
if supported; false
otherwiseprotected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
AbstractHttpMessageConverter
AbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.readInternal
in class AbstractHttpMessageConverter<Object>
clazz
- the type of object to returninputMessage
- the HTTP input message to read fromIOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errorspublic Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
GenericHttpMessageConverter
read
in interface GenericHttpMessageConverter<Object>
type
- the type of object to return. This type must have previously
been passed to the canRead
method of this interface,
which must have returned true
.contextClass
- a context class for the target type, for example a class
in which the target type appears in a method signature (can be null
)inputMessage
- the HTTP input message to read fromIOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errorsprotected com.google.gson.reflect.TypeToken<?> getTypeToken(Type type)
TypeToken
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); } }
type
- the type for which to return the TypeTokenprotected void writeInternal(Object o, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
AbstractGenericHttpMessageConverter
AbstractGenericHttpMessageConverter.write(T, java.lang.reflect.Type, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.writeInternal
in class AbstractGenericHttpMessageConverter<Object>
o
- the object to write to the output messagetype
- the type of object to write, can be null
if not specified.outputMessage
- the HTTP output message to write toIOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errors