public class GsonHttpMessageConverter extends AbstractGenericHttpMessageConverter<java.lang.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 HashMaps.
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 java.nio.charset.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(java.lang.reflect.Type type)
Deprecated.
as of Spring Framework 4.3.8, in favor of signature-based resolution
|
java.lang.Object |
read(java.lang.reflect.Type type,
java.lang.Class<?> contextClass,
HttpInputMessage inputMessage)
Read an object of the given type form the given input message, and returns it.
|
protected java.lang.Object |
readInternal(java.lang.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(java.lang.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(java.lang.Object o,
java.lang.reflect.Type type,
HttpOutputMessage outputMessage)
Abstract template method that writes the actual body.
|
canRead, canWrite, supports, write, writeInternaladdDefaultHeaders, canRead, canRead, canWrite, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, writeclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcanRead, canWrite, getSupportedMediaTypes, read, writepublic 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(java.lang.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 java.lang.Object read(java.lang.reflect.Type type,
java.lang.Class<?> contextClass,
HttpInputMessage inputMessage)
throws java.io.IOException,
HttpMessageNotReadableException
GenericHttpMessageConvertertype - 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 fromjava.io.IOException - in case of I/O errorsHttpMessageNotReadableException - in case of conversion errorsprotected java.lang.Object readInternal(java.lang.Class<?> clazz,
HttpInputMessage inputMessage)
throws java.io.IOException,
HttpMessageNotReadableException
AbstractHttpMessageConverterAbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage).readInternal in class AbstractHttpMessageConverter<java.lang.Object>clazz - the type of object to returninputMessage - the HTTP input message to read fromjava.io.IOException - in case of I/O errorsHttpMessageNotReadableException - in case of conversion errors@Deprecated protected com.google.gson.reflect.TypeToken<?> getTypeToken(java.lang.reflect.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(java.lang.Object o,
java.lang.reflect.Type type,
HttpOutputMessage outputMessage)
throws java.io.IOException,
HttpMessageNotWritableException
AbstractGenericHttpMessageConverterAbstractGenericHttpMessageConverter.write(T, java.lang.reflect.Type, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage).writeInternal in class AbstractGenericHttpMessageConverter<java.lang.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 tojava.io.IOException - in case of I/O errorsHttpMessageNotWritableException - in case of conversion errors