public class GsonHttpMessageConverter extends AbstractJsonHttpMessageConverter
HttpMessageConverter
that can read and write JSON using the
Google Gson library.
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.
Gson,
GsonBuilder,
setGson(com.google.gson.Gson)DEFAULT_CHARSETlogger| Constructor and Description |
|---|
GsonHttpMessageConverter()
Construct a new
GsonHttpMessageConverter with default configuration. |
GsonHttpMessageConverter(com.google.gson.Gson gson)
Construct a new
GsonHttpMessageConverter with the given delegate. |
| Modifier and Type | Method and Description |
|---|---|
com.google.gson.Gson |
getGson()
Return the configured
Gson instance for this converter. |
protected Object |
readInternal(Type resolvedType,
Reader reader)
Template method that reads the JSON-bound object from the given
Reader. |
void |
setGson(com.google.gson.Gson gson)
Set the
Gson instance to use. |
protected void |
writeInternal(Object object,
Type type,
Writer writer)
Template method that writes the JSON-bound object to the given
Writer. |
read, readInternal, setJsonPrefix, setPrefixJson, writeInternalcanRead, 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, getSupportedMediaTypes, read, writepublic GsonHttpMessageConverter()
GsonHttpMessageConverter with default configuration.public GsonHttpMessageConverter(com.google.gson.Gson gson)
GsonHttpMessageConverter with the given delegate.gson - the Gson instance to usepublic 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.
GsonHttpMessageConverter(Gson)public com.google.gson.Gson getGson()
Gson instance for this converter.protected Object readInternal(Type resolvedType, Reader reader) throws Exception
AbstractJsonHttpMessageConverterReader.readInternal in class AbstractJsonHttpMessageConverterresolvedType - the resolved generic typereader - the Reader to useException - in case of read/parse failuresprotected void writeInternal(Object object, @Nullable Type type, Writer writer) throws Exception
AbstractJsonHttpMessageConverterWriter.writeInternal in class AbstractJsonHttpMessageConverterobject - the object to write to the output messagetype - the type of object to write (may be null)writer - the Writer to useException - in case of write failures