public class MappingJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter
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.
DEFAULT_CHARSET, defaultObjectMapper
logger
Constructor and Description |
---|
MappingJackson2HttpMessageConverter()
Construct a new
MappingJackson2HttpMessageConverter using default configuration
provided by Jackson2ObjectMapperBuilder . |
MappingJackson2HttpMessageConverter(ObjectMapper objectMapper)
Construct a new
MappingJackson2HttpMessageConverter with a custom ObjectMapper . |
Modifier and Type | Method and Description |
---|---|
void |
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.
|
canRead, canRead, canWrite, getCharset, getContentLength, getDefaultContentType, getJavaType, getJsonEncoding, getObjectMapper, getObjectMappersForType, getSupportedMediaTypes, logWarningIfNecessary, read, readInternal, registerObjectMappersForType, setObjectMapper, setPrettyPrint, writeInternal, writeSuffix
canWrite, supports, write, writeInternal
addDefaultHeaders, canRead, canWrite, getDefaultCharset, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getSupportedMediaTypes, read, write
public MappingJackson2HttpMessageConverter()
MappingJackson2HttpMessageConverter
using default configuration
provided by Jackson2ObjectMapperBuilder
.public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper)
MappingJackson2HttpMessageConverter
with a custom ObjectMapper
.
You can use Jackson2ObjectMapperBuilder
to build it easily.Jackson2ObjectMapperBuilder.json()
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)
protected void writePrefix(JsonGenerator generator, Object object) throws IOException
AbstractJackson2HttpMessageConverter
writePrefix
in class AbstractJackson2HttpMessageConverter
generator
- the generator to use for writing content.object
- the object to write to the output message.IOException