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:
- 
Field SummaryFieldsFields inherited from class org.springframework.http.converter.AbstractHttpMessageConverterlogger
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionfinal Objectread(Type type, Class<?> contextClass, HttpInputMessage inputMessage) Read an object of the given type form 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, Type type, Writer writer) Template method that writes the JSON-bound object to the givenWriter.protected final voidwriteInternal(Object object, Type type, HttpOutputMessage outputMessage) Abstract template method that writes the actual body.Methods inherited from class org.springframework.http.converter.AbstractGenericHttpMessageConvertercanRead, canWrite, supports, write, writeInternalMethods inherited from class org.springframework.http.converter.AbstractHttpMessageConverteraddDefaultHeaders, canRead, canRead, canWrite, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, supportsRepeatableWrites, writeMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.http.converter.HttpMessageConvertercanRead, canWrite, getSupportedMediaTypes, getSupportedMediaTypes, read, write
- 
Field Details- 
DEFAULT_CHARSETThe default charset used by the converter.
 
- 
- 
Constructor Details- 
AbstractJsonHttpMessageConverterpublic AbstractJsonHttpMessageConverter()
 
- 
- 
Method Details- 
setJsonPrefixSpecify a custom prefix to use for JSON output. Default is none.- See Also:
 
- 
setPrefixJsonpublic 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:
 
- 
readpublic final Object read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException Description copied from interface:GenericHttpMessageConverterRead 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 the- canReadmethod 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 from
- Returns:
- the converted object
- Throws:
- IOException- in case of I/O errors
- HttpMessageNotReadableException- in case of conversion errors
 
- 
readInternalprotected 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(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage).- Specified by:
- readInternalin class- AbstractHttpMessageConverter<Object>
- Parameters:
- clazz- the type of object to return
- inputMessage- the HTTP input message to read from
- Returns:
- the converted object
- Throws:
- IOException- in case of I/O errors
- HttpMessageNotReadableException- in case of conversion errors
 
- 
writeInternalprotected 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, java.lang.reflect.Type, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage).- Specified by:
- writeInternalin class- AbstractGenericHttpMessageConverter<Object>
- Parameters:
- object- the object to write to the output message
- type- the type of object to write (may be- null)
- outputMessage- the HTTP output message to write to
- Throws:
- IOException- in case of I/O errors
- HttpMessageNotWritableException- in case of conversion errors
 
- 
readInternalTemplate method that reads the JSON-bound object from the givenReader.- Parameters:
- resolvedType- the resolved generic type
- reader- the Reader to use
- Returns:
- the JSON-bound object
- Throws:
- Exception- in case of read/parse failures
 
- 
writeInternalprotected 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 message
- type- the type of object to write (may be- null)
- writer- the Writer to use
- Throws:
- Exception- in case of write failures
 
 
-