spring-framework / org.springframework.http.converter.json / MappingJackson2HttpMessageConverter

MappingJackson2HttpMessageConverter

open class MappingJackson2HttpMessageConverter : AbstractJackson2HttpMessageConverter

Implementation of org.springframework.http.converter.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.

Author
Arjen Poutsma

Author
Keith Donald

Author
Rossen Stoyanchev

Author
Juergen Hoeller

Author
Sebastien Deleuze

Since
3.1.2

Constructors

<init>

MappingJackson2HttpMessageConverter()

Construct a new MappingJackson2HttpMessageConverter using default configuration provided by Jackson2ObjectMapperBuilder.

MappingJackson2HttpMessageConverter(objectMapper: ObjectMapper)

Construct a new MappingJackson2HttpMessageConverter with a custom ObjectMapper. You can use Jackson2ObjectMapperBuilder to build it easily.

Inherited Properties

DEFAULT_CHARSET

static val DEFAULT_CHARSET: Charset

Functions

setJsonPrefix

open fun setJsonPrefix(jsonPrefix: String): Unit

Specify a custom prefix to use for this view's JSON output. Default is none.

setPrefixJson

open fun setPrefixJson(prefixJson: Boolean): Unit

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.

Inherited Functions

canRead

open fun canRead(clazz: Class<*>, mediaType: MediaType): Boolean
open fun canRead(type: Type, contextClass: Class<*>, mediaType: MediaType): Boolean

canWrite

open fun canWrite(clazz: Class<*>, mediaType: MediaType): Boolean

getObjectMapper

open fun getObjectMapper(): ObjectMapper

Return the underlying ObjectMapper for this view.

read

open fun read(type: Type, contextClass: Class<*>, inputMessage: HttpInputMessage): Any

setObjectMapper

open fun setObjectMapper(objectMapper: ObjectMapper): Unit

Set the ObjectMapper for this view. If not set, a default ObjectMapper is used.

Setting a custom-configured ObjectMapper is one way to take further control of the JSON serialization process. For example, an extended com.fasterxml.jackson.databind.ser.SerializerFactory can be configured that provides custom serializers for specific types. The other option for refining the serialization process is to use Jackson's provided annotations on the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.

setPrettyPrint

open fun setPrettyPrint(prettyPrint: Boolean): Unit

Whether to use the DefaultPrettyPrinter when writing JSON. This is a shortcut for setting up an ObjectMapper as follows:

 ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); converter.setObjectMapper(mapper);