Class MappingJackson2HttpMessageConverter
java.lang.Object
org.springframework.http.converter.AbstractHttpMessageConverter<T>
org.springframework.http.converter.AbstractGenericHttpMessageConverter<Object>
org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
- All Implemented Interfaces:
GenericHttpMessageConverter<Object>
,HttpMessageConverter<Object>
Implementation of
HttpMessageConverter
that can read and
write JSON using Jackson 2.x's ObjectMapper
.
This converter can be used to bind to typed beans, or untyped HashMap
instances.
By default, this converter supports application/json
and application/*+json
with UTF-8
character set. This can be overridden by setting the
supportedMediaTypes
property.
The default constructor uses the default configuration provided by Jackson2ObjectMapperBuilder
.
Compatible with Jackson 2.9 to 2.12, as of Spring 5.3.
- Since:
- 3.1.2
- Author:
- Arjen Poutsma, Keith Donald, Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
-
Field Summary
Fields inherited from class org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter
defaultObjectMapper
Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
logger
-
Constructor Summary
ConstructorDescriptionConstruct a newMappingJackson2HttpMessageConverter
using default configuration provided byJackson2ObjectMapperBuilder
.MappingJackson2HttpMessageConverter
(ObjectMapper objectMapper) Construct a newMappingJackson2HttpMessageConverter
with a customObjectMapper
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
setJsonPrefix
(String jsonPrefix) Specify a custom prefix to use for this view's JSON output.void
setPrefixJson
(boolean prefixJson) Indicate whether the JSON output by this view should be prefixed with ")]}', ".protected void
writePrefix
(JsonGenerator generator, Object object) Write a prefix before the main content.Methods inherited from class org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter
canRead, canRead, canWrite, getCharset, getContentLength, getDefaultContentType, getJavaType, getJsonEncoding, getObjectMapper, getObjectMappersForType, getSupportedMediaTypes, logWarningIfNecessary, read, readInternal, registerObjectMappersForType, setObjectMapper, setPrettyPrint, writeInternal, writeSuffix
Methods inherited from class org.springframework.http.converter.AbstractGenericHttpMessageConverter
canWrite, supports, write, writeInternal
Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
addDefaultHeaders, canRead, canWrite, getDefaultCharset, 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
getSupportedMediaTypes, read, write
-
Constructor Details
-
MappingJackson2HttpMessageConverter
public MappingJackson2HttpMessageConverter()Construct a newMappingJackson2HttpMessageConverter
using default configuration provided byJackson2ObjectMapperBuilder
. -
MappingJackson2HttpMessageConverter
Construct a newMappingJackson2HttpMessageConverter
with a customObjectMapper
. You can useJackson2ObjectMapperBuilder
to build it easily.- See Also:
-
-
Method Details
-
setJsonPrefix
Specify a custom prefix to use for this view's 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 is 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.
- See Also:
-
writePrefix
Description copied from class:AbstractJackson2HttpMessageConverter
Write a prefix before the main content.- Overrides:
writePrefix
in classAbstractJackson2HttpMessageConverter
- Parameters:
generator
- the generator to use for writing content.object
- the object to write to the output message.- Throws:
IOException
-