spring-framework / org.springframework.web.servlet.view.json / MappingJackson2JsonView

MappingJackson2JsonView

open class MappingJackson2JsonView : AbstractJackson2View

Spring MVC View that renders JSON content by serializing the model for the current request using Jackson 2's ObjectMapper.

By default, the entire contents of the model map (with the exception of framework-specific classes) will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON alone via #setExtractValueFromSingleKeyModel.

The default constructor uses the default configuration provided by Jackson2ObjectMapperBuilder.

Compatible with Jackson 2.6 and higher, as of Spring 4.3.

Author
Jeremy Grelle

Author
Arjen Poutsma

Author
Rossen Stoyanchev

Author
Juergen Hoeller

Author
Sebastien Deleuze

Since
3.1.2

Constructors

<init>

MappingJackson2JsonView()

Construct a new MappingJackson2JsonView using default configuration provided by Jackson2ObjectMapperBuilder and setting the content type to application/json.

MappingJackson2JsonView(objectMapper: ObjectMapper)

Construct a new MappingJackson2JsonView using the provided ObjectMapper and setting the content type to application/json.

Properties

DEFAULT_CONTENT_TYPE

static val DEFAULT_CONTENT_TYPE: String

Default content type: "application/json". Overridable through #setContentType.

DEFAULT_JSONP_CONTENT_TYPE

static val DEFAULT_JSONP_CONTENT_TYPE: String

Default content type for JSONP: "application/javascript".

Functions

getModelKeys

fun getModelKeys(): MutableSet<String>

Return the attributes in the model that should be rendered by this view.

setExtractValueFromSingleKeyModel

open fun setExtractValueFromSingleKeyModel(extractValueFromSingleKeyModel: Boolean): Unit

Set whether to serialize models containing a single attribute as a map or whether to extract the single value from the model and serialize it directly.

The effect of setting this flag is similar to using MappingJackson2HttpMessageConverter with an @ResponseBody request-handling method.

Default is false.

setJsonPrefix

open fun setJsonPrefix(jsonPrefix: String): Unit

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

setJsonpParameterNames

open fun setJsonpParameterNames(jsonpParameterNames: MutableSet<String>): Unit

Set JSONP request parameter names. Each time a request has one of those parameters, the resulting JSON will be wrapped into a function named as specified by the JSONP request parameter value.

The parameter names configured by default are "jsonp" and "callback".

setModelKey

open fun setModelKey(modelKey: String): Unit

{@inheritDoc}

setModelKeys

open fun setModelKeys(modelKeys: MutableSet<String>): Unit

Set the attributes in the model that should be rendered by this view. When set, all other model attributes will be ignored.

setPrefixJson

open fun setPrefixJson(prefixJson: Boolean): Unit

Indicates 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.