public class MappingJackson2JsonView extends AbstractJackson2View
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
.
Compatible with Jackson 2.1 and higher.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_CONTENT_TYPE
Default content type: "application/json".
|
static java.lang.String |
DEFAULT_JSONP_CONTENT_TYPE
Default content type for JSONP: "application/javascript".
|
updateContentLength
logger
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
Constructor and Description |
---|
MappingJackson2JsonView()
Construct a new
MappingJackson2JsonView using default configuration
provided by Jackson2ObjectMapperBuilder and setting the content type
to application/json . |
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
filterAndWrapModel(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request)
Filter and optionally wrap the model in
MappingJacksonValue container. |
protected java.lang.Object |
filterModel(java.util.Map<java.lang.String,java.lang.Object> model)
Filter out undesired attributes from the given model.
|
java.util.Set<java.lang.String> |
getModelKeys()
Return the attributes in the model that should be rendered by this view.
|
java.util.Set<java.lang.String> |
getRenderedAttributes()
Deprecated.
use
getModelKeys() instead |
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 |
setJsonpParameterNames(java.util.Set<java.lang.String> jsonpParameterNames)
Set JSONP request parameter names.
|
void |
setJsonPrefix(java.lang.String jsonPrefix)
Specify a custom prefix to use for this view's JSON output.
|
void |
setModelKey(java.lang.String modelKey)
Set the attribute in the model that should be rendered by this view.
|
void |
setModelKeys(java.util.Set<java.lang.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 "{} && ".
|
void |
setRenderedAttributes(java.util.Set<java.lang.String> renderedAttributes)
Deprecated.
use
setModelKeys(Set) instead |
protected void |
setResponseContentType(HttpServletRequest request,
HttpServletResponse response)
Set the content type of the response to the configured
content type unless the
View.SELECTED_CONTENT_TYPE request attribute is present and set
to a concrete media type. |
protected void |
writePrefix(JsonGenerator generator,
java.lang.Object object)
Write a prefix before the main content.
|
protected void |
writeSuffix(JsonGenerator generator,
java.lang.Object object)
Write a suffix after the main content.
|
getEncoding, getObjectMapper, prepareResponse, renderMergedOutputModel, setDisableCaching, setEncoding, setObjectMapper, setPrettyPrint, setUpdateContentLength, writeContent
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, toString, writeToResponse
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
public static final java.lang.String DEFAULT_CONTENT_TYPE
AbstractView.setContentType(java.lang.String)
.public static final java.lang.String DEFAULT_JSONP_CONTENT_TYPE
public MappingJackson2JsonView()
MappingJackson2JsonView
using default configuration
provided by Jackson2ObjectMapperBuilder
and setting the content type
to application/json
.public void setJsonPrefix(java.lang.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 does not affect the evaluation of JSON, but if JSON validation is performed on the string, the prefix would need to be ignored.
setJsonPrefix(java.lang.String)
public void setModelKey(java.lang.String modelKey)
setModelKey
in class AbstractJackson2View
public void setModelKeys(java.util.Set<java.lang.String> modelKeys)
public final java.util.Set<java.lang.String> getModelKeys()
@Deprecated public void setRenderedAttributes(java.util.Set<java.lang.String> renderedAttributes)
setModelKeys(Set)
instead@Deprecated public final java.util.Set<java.lang.String> getRenderedAttributes()
getModelKeys()
insteadpublic void setExtractValueFromSingleKeyModel(boolean extractValueFromSingleKeyModel)
The effect of setting this flag is similar to using MappingJackson2HttpMessageConverter
with an @ResponseBody
request-handling method.
Default is false
.
public void setJsonpParameterNames(java.util.Set<java.lang.String> jsonpParameterNames)
The parameter names configured by default are "jsonp" and "callback".
protected java.lang.Object filterModel(java.util.Map<java.lang.String,java.lang.Object> model)
Map
or a single value object.
The default implementation removes BindingResult
instances and entries
not included in the renderedAttributes
property.
filterModel
in class AbstractJackson2View
model
- the model, as passed on to AbstractJackson2View.renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected java.lang.Object filterAndWrapModel(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request)
AbstractJackson2View
MappingJacksonValue
container.filterAndWrapModel
in class AbstractJackson2View
model
- the model, as passed on to AbstractJackson2View.renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
request
- current HTTP requestprotected void writePrefix(JsonGenerator generator, java.lang.Object object) throws java.io.IOException
AbstractJackson2View
writePrefix
in class AbstractJackson2View
generator
- the generator to use for writing content.object
- the object to write to the output message.java.io.IOException
protected void writeSuffix(JsonGenerator generator, java.lang.Object object) throws java.io.IOException
AbstractJackson2View
writeSuffix
in class AbstractJackson2View
generator
- the generator to use for writing content.object
- the object to write to the output message.java.io.IOException
protected void setResponseContentType(HttpServletRequest request, HttpServletResponse response)
AbstractView
content type
unless the
View.SELECTED_CONTENT_TYPE
request attribute is present and set
to a concrete media type.setResponseContentType
in class AbstractView