public abstract class AbstractJsonHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object>
Note that the Jackson converters have a dedicated class hierarchy due to their multi-format support.
GsonHttpMessageConverter
,
JsonbHttpMessageConverter
,
readInternal(Type, Reader)
,
writeInternal(Object, Type, Writer)
Modifier and Type | Field and Description |
---|---|
static Charset |
DEFAULT_CHARSET
The default charset used by the converter.
|
logger
Constructor and Description |
---|
AbstractJsonHttpMessageConverter() |
Modifier and Type | Method and 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 given
Reader . |
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.
|
protected abstract void |
writeInternal(Object o,
Type type,
Writer writer)
Template method that writes the JSON-bound object to the given
Writer . |
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 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 final Object read(Type type, @Nullable 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 final 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 errorsprotected final void writeInternal(Object o, @Nullable 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 errorsprotected abstract Object readInternal(Type resolvedType, Reader reader) throws Exception
Reader
.resolvedType
- the resolved generic typereader
- the Reader to useException
- in case of read/parse failuresprotected abstract void writeInternal(Object o, @Nullable Type type, Writer writer) throws Exception
Writer
.o
- 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