public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverter<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
with
UTF-8
character set.
Tested against Gson 2.8; 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 |
---|---|
com.google.gson.Gson |
getGson()
Return the configured
Gson 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 resolution
|
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 void |
writeInternal(Object o,
Type type,
HttpOutputMessage outputMessage)
Abstract template method that writes the actual body.
|
canRead, canWrite, supports, write, writeInternal
addDefaultHeaders, canRead, canRead, canWrite, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
canRead, canWrite, 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 will be 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 Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
GenericHttpMessageConverter
type
- the (potentially generic) 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 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 errors@Deprecated protected 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 (may be null
)outputMessage
- the HTTP output message to write toIOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errors