Package org.springframework.web.servlet.view.json

# Class MappingJackson2JsonView

java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractView
org.springframework.web.servlet.view.json.AbstractJackson2View
org.springframework.web.servlet.view.json.MappingJackson2JsonView

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanNameAware`, `org.springframework.context.ApplicationContextAware`, `org.springframework.web.context.ServletContextAware`, `View`

public class MappingJackson2JsonView extends 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(boolean)`.

The default constructor uses the default configuration provided by `Jackson2ObjectMapperBuilder`.

Since:
  3.1.2
Author:
  Jeremy Grelle, Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`DEFAULT_CONTENT_TYPE`
Default content type: "application/json".

### Fields inherited from class org.springframework.web.servlet.view.json.AbstractJackson2View

`updateContentLength`

### Fields inherited from class org.springframework.context.support.ApplicationObjectSupport

`logger`

### Fields inherited from interface org.springframework.web.servlet.View

`PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE`

## Constructor Summary

Constructors
Constructor
Description
`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`.

## Method Summary

Modifier and Type
Method
Description
`protected Object`
`filterModel(Map<String,Object> model)`
Filter out undesired attributes from the given model.
`final Set<String>`
`getModelKeys()`
Return the attributes in the model that should be rendered by this view.
`void`
`setExtractValueFromSingleKeyModel(boolean extractValueFromSingleKeyModel)`
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.
`void`
`setJsonPrefix(String jsonPrefix)`
Specify a custom prefix to use for this view's JSON output.
`void`
`setModelKey(String modelKey)`
Set the attribute in the model that should be rendered by this view.
`void`
`setModelKeys(Set<String> modelKeys)`
Set the attributes in the model that should be rendered by this view.
`void`
`setPrefixJson(boolean prefixJson)`
Indicates 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.web.servlet.view.json.AbstractJackson2View

`filterAndWrapModel, getEncoding, getObjectMapper, prepareResponse, renderMergedOutputModel, setDisableCaching, setEncoding, setObjectMapper, setPrettyPrint, setUpdateContentLength, writeContent, writeSuffix`

### Methods inherited from class org.springframework.web.servlet.view.AbstractView

`addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, formatViewName, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, toString, writeToResponse`

### Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport

`getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext`

### Methods inherited from class org.springframework.context.support.ApplicationObjectSupport

`getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Field Details

### DEFAULT_CONTENT_TYPE

public static final String DEFAULT_CONTENT_TYPE
Default content type: "application/json". Overridable through `AbstractView.setContentType(java.lang.String)`.

See Also:
  - Constant Field Values

## Constructor Details

### MappingJackson2JsonView

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

### MappingJackson2JsonView

public MappingJackson2JsonView(ObjectMapper objectMapper)
Construct a new `MappingJackson2JsonView` using the provided `ObjectMapper` and setting the content type to `application/json`.

Since:
  4.2.1

## Method Details

### setJsonPrefix

public void setJsonPrefix(String jsonPrefix)
Specify a custom prefix to use for this view's JSON output. Default is none.

See Also:
  - `setPrefixJson(boolean)`

### setPrefixJson

public void setPrefixJson(boolean prefixJson)
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.

See Also:
  - `setJsonPrefix(java.lang.String)`

### setModelKey

public void setModelKey(String modelKey)
Description copied from class: `AbstractJackson2View`
Set the attribute in the model that should be rendered by this view. When set, all other model attributes will be ignored.

Specified by:
  `setModelKey` in class `AbstractJackson2View`

### setModelKeys

public void setModelKeys(@Nullable Set<String> modelKeys)
Set the attributes in the model that should be rendered by this view. When set, all other model attributes will be ignored.

### getModelKeys

@Nullable public final Set<String> getModelKeys()
Return the attributes in the model that should be rendered by this view.

### setExtractValueFromSingleKeyModel

public void setExtractValueFromSingleKeyModel(boolean extractValueFromSingleKeyModel)
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`.

### filterModel

protected Object filterModel(Map<String,Object> model)
Filter out undesired attributes from the given model. The return value can be either another `Map` or a single value object.

The default implementation removes `BindingResult` instances and entries not included in the `modelKeys` property.

Specified by:
  `filterModel` in class `AbstractJackson2View`
Parameters:
  `model` - the model, as passed on to `AbstractJackson2View.renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)`
Returns:
  the value to be rendered

### writePrefix

protected void writePrefix(JsonGenerator generator, Object object) throws IOException
Description copied from class: `AbstractJackson2View`
Write a prefix before the main content.

Overrides:
  `writePrefix` in class `AbstractJackson2View`
Parameters:
  `generator` - the generator to use for writing content.
  `object` - the object to write to the output message.
Throws:
  `IOException`