Class AbstractJsonHttpMessageConverter
java.lang.Object
org.springframework.http.converter.AbstractHttpMessageConverter<Object>
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, for example, 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:
-
Field Summary
FieldsFields inherited from class AbstractHttpMessageConverter
logger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal Objectread(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage) Read an object of the given type from the given input message, and returns it.protected final ObjectreadInternal(Class<?> clazz, HttpInputMessage inputMessage) Abstract template method that reads the actual object.protected abstract ObjectreadInternal(Type resolvedType, Reader reader) Template method that reads the JSON-bound object from the givenReader.voidsetJsonPrefix(String jsonPrefix) Specify a custom prefix to use for JSON output.voidsetPrefixJson(boolean prefixJson) Indicate whether the JSON output by this view should be prefixed with ")]}', ".protected abstract voidwriteInternal(Object object, @Nullable Type type, Writer writer) Template method that writes the JSON-bound object to the givenWriter.protected final voidwriteInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage) Abstract template method that writes the actual body.Methods inherited from class AbstractGenericHttpMessageConverter
canRead, canWrite, supports, write, writeInternalMethods inherited from class AbstractHttpMessageConverter
addDefaultHeaders, canRead, canRead, canWrite, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, supportsRepeatableWrites, writeMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface HttpMessageConverter
canRead, canWrite, getSupportedMediaTypes, getSupportedMediaTypes, read, write
-
Field Details
-
DEFAULT_CHARSET
The default charset used by the converter.
-
-
Constructor Details
-
AbstractJsonHttpMessageConverter
public AbstractJsonHttpMessageConverter()
-
-
Method Details
-
setJsonPrefix
Specify a custom prefix to use for JSON output. Default is none.- See Also:
-
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:
-
read
public final Object read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException Description copied from interface:GenericHttpMessageConverterRead an object of the given type from 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 thecanReadmethod 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:AbstractHttpMessageConverterAbstract template method that reads the actual object. Invoked fromAbstractHttpMessageConverter.read(Class, HttpInputMessage).- Specified by:
readInternalin 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:AbstractGenericHttpMessageConverterAbstract template method that writes the actual body. Invoked fromAbstractGenericHttpMessageConverter.write(T, Type, MediaType, HttpOutputMessage).- Specified by:
writeInternalin 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
-
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- theWriterto use- Throws:
Exception- in case of write failures
-