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 HashMap
s.
By default, it supports application/json
and application/*+json
with
UTF-8
character set.
Tested against Gson 2.6; compatible with Gson 2.0 and higher.
com.google.gson.Gson
,
com.google.gson.GsonBuilder
,
setGson(Gson)
Modifier and Type | Field and Description |
---|---|
private Gson |
gson |
DEFAULT_CHARSET
logger
Constructor and Description |
---|
GsonHttpMessageConverter()
Construct a new
GsonHttpMessageConverter with default configuration. |
GsonHttpMessageConverter(Gson gson)
Construct a new
GsonHttpMessageConverter with the given delegate. |
Modifier and Type | Method and Description |
---|---|
Gson |
getGson()
Return the configured
Gson instance for this converter. |
protected java.lang.Object |
readInternal(java.lang.reflect.Type resolvedType,
java.io.Reader reader)
Template method that reads the JSON-bound object from the given
Reader . |
void |
setGson(Gson gson)
Set the
Gson instance to use. |
protected void |
writeInternal(java.lang.Object o,
java.lang.reflect.Type type,
java.io.Writer writer)
Template method that writes the JSON-bound object to the given
Writer . |
read, readInternal, setJsonPrefix, setPrefixJson, writeInternal
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 GsonHttpMessageConverter()
GsonHttpMessageConverter
with default configuration.public GsonHttpMessageConverter(Gson gson)
GsonHttpMessageConverter
with the given delegate.gson
- the Gson instance to usepublic void setGson(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 Gson getGson()
Gson
instance for this converter.protected java.lang.Object readInternal(java.lang.reflect.Type resolvedType, java.io.Reader reader) throws java.lang.Exception
AbstractJsonHttpMessageConverter
Reader
.readInternal
in class AbstractJsonHttpMessageConverter
resolvedType
- the resolved generic typereader
- the Reader to usejava.lang.Exception
- in case of read/parse failuresprotected void writeInternal(java.lang.Object o, @Nullable java.lang.reflect.Type type, java.io.Writer writer) throws java.lang.Exception
AbstractJsonHttpMessageConverter
Writer
.writeInternal
in class AbstractJsonHttpMessageConverter
o
- the object to write to the output messagetype
- the type of object to write (may be null
)writer
- the Writer to usejava.lang.Exception
- in case of write failures