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 and higher, as of Spring 5.0.
DEFAULT_CHARSET, objectMapper
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.
|
protected void |
writeSuffix(JsonGenerator generator,
Object object)
Write a suffix after the main content.
|
canRead, canRead, canWrite, getContentLength, getDefaultContentType, getJavaType, getJsonEncoding, getObjectMapper, logWarningIfNecessary, read, readInternal, setObjectMapper, setPrettyPrint, writeInternal
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)
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
protected void writeSuffix(JsonGenerator generator, Object object) throws IOException
AbstractJackson2HttpMessageConverter
writeSuffix
in class AbstractJackson2HttpMessageConverter
generator
- the generator to use for writing content.object
- the object to write to the output message.IOException