Class AbstractJsonHttpMessageConverter
- java.lang.Object
- org.springframework.http.converter.AbstractHttpMessageConverter<T>
- org.springframework.http.converter.AbstractGenericHttpMessageConverter<Object>
- org.springframework.http.converter.json.AbstractJsonHttpMessageConverter
- All Implemented Interfaces:
GenericHttpMessageConverter<Object>
,HttpMessageConverter<Object>
- Direct Known Subclasses:
GsonHttpMessageConverter
,JsonbHttpMessageConverter
public abstract class AbstractJsonHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object>
Common base class for plain JSON converters, e.g. Gson and JSON-B.Note that the Jackson converters have a dedicated class hierarchy due to their multi-format support.
- Since:
- 5.0
- Author:
- Juergen Hoeller
- See Also:
GsonHttpMessageConverter
,JsonbHttpMessageConverter
,readInternal(Type, Reader)
,writeInternal(Object, Type, Writer)
Field Summary
Fields Modifier and Type Field Description static Charset
DEFAULT_CHARSET
The default charset used by the converter.Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
logger
Constructor Summary
Constructors Constructor Description AbstractJsonHttpMessageConverter()
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description 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.protected abstract Object
readInternal(Type resolvedType, Reader reader)
Template method that reads the JSON-bound object from the givenReader
.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 abstract void
writeInternal(Object object, Type type, Writer writer)
Template method that writes the JSON-bound object to the givenWriter
.protected void
writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
Abstract template method that writes the actual body.Methods inherited from class org.springframework.http.converter.AbstractGenericHttpMessageConverter
canRead, canWrite, supports, write, writeInternal
Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
addDefaultHeaders, canRead, canRead, canWrite, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.http.converter.HttpMessageConverter
canRead, canWrite, getSupportedMediaTypes, read, write
Field Detail
DEFAULT_CHARSET
public static final Charset DEFAULT_CHARSET
The default charset used by the converter.
Constructor Detail
AbstractJsonHttpMessageConverter
public AbstractJsonHttpMessageConverter()
Method Detail
setJsonPrefix
public void setJsonPrefix(String jsonPrefix)
Specify a custom prefix to use for JSON output. Default is none.- See Also:
setPrefixJson(boolean)
setPrefixJson
public void setPrefixJson(boolean prefixJson)
Indicate whether the JSON output by this view should be prefixed with ")]}', ". Default isfalse
.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.
- See Also:
setJsonPrefix(java.lang.String)
read
public final Object read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
Description copied from interface:GenericHttpMessageConverter
Read an object of the given type form the given input message, and returns it.- Parameters:
type
- the (potentially generic) type of object to return. This type must have previously been passed to thecanRead
method of this interface, which must have returnedtrue
.contextClass
- a context class for the target type, for example a class in which the target type appears in a method signature (can benull
)inputMessage
- the HTTP input message to read from- Returns:
- the converted object
- Throws:
IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errors
readInternal
protected final Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
Description copied from class:AbstractHttpMessageConverter
Abstract template method that reads the actual object. Invoked fromAbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.- Specified by:
readInternal
in classAbstractHttpMessageConverter<Object>
- Parameters:
clazz
- the type of object to returninputMessage
- the HTTP input message to read from- Returns:
- the converted object
- Throws:
IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errors
writeInternal
protected final void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
Description copied from class:AbstractGenericHttpMessageConverter
Abstract template method that writes the actual body. Invoked fromAbstractGenericHttpMessageConverter.write(T, java.lang.reflect.Type, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.- Specified by:
writeInternal
in classAbstractGenericHttpMessageConverter<Object>
- Parameters:
object
- the object to write to the output messagetype
- the type of object to write (may benull
)outputMessage
- the HTTP output message to write to- Throws:
IOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errors
readInternal
protected abstract Object readInternal(Type resolvedType, Reader reader) throws Exception
Template method that reads the JSON-bound object from the givenReader
.- Parameters:
resolvedType
- the resolved generic typereader
- the Reader to use- Returns:
- the JSON-bound object
- Throws:
Exception
- in case of read/parse failures
writeInternal
protected abstract void writeInternal(Object object, @Nullable Type type, Writer writer) throws Exception
Template method that writes the JSON-bound object to the givenWriter
.- Parameters:
object
- the object to write to the output messagetype
- the type of object to write (may benull
)writer
- the Writer to use- Throws:
Exception
- in case of write failures