public class MappingJackson2JsonView
extends org.springframework.web.servlet.view.AbstractView
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).
| 限定符和类型 | 字段和说明 |
|---|---|
static java.lang.String |
DEFAULT_CONTENT_TYPE
Default content type: "application/json".
|
| 构造器和说明 |
|---|
MappingJackson2JsonView()
Construct a new
MappingJackson2JsonView, setting the content type to application/json. |
| 限定符和类型 | 方法和说明 |
|---|---|
protected java.lang.Object |
filterModel(java.util.Map<java.lang.String,java.lang.Object> model)
Filter out undesired attributes from the given model.
|
com.fasterxml.jackson.core.JsonEncoding |
getEncoding()
Return the
JsonEncoding for this view. |
java.util.Set<java.lang.String> |
getModelKeys()
Return the attributes in the model that should be rendered by this view.
|
com.fasterxml.jackson.databind.ObjectMapper |
getObjectMapper()
Return the
ObjectMapper for this view. |
java.util.Set<java.lang.String> |
getRenderedAttributes()
已过时。
use
getModelKeys() instead |
protected void |
prepareResponse(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) |
protected void |
renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) |
void |
setDisableCaching(boolean disableCaching)
Disables caching of the generated JSON.
|
void |
setEncoding(com.fasterxml.jackson.core.JsonEncoding encoding)
Set the
JsonEncoding for 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(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 |
setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Set the
ObjectMapper for this view. |
void |
setPrefixJson(boolean prefixJson)
Indicates whether the JSON output by this view should be prefixed with "{} && ".
|
void |
setPrettyPrint(boolean prettyPrint)
Whether to use the default pretty printer when writing JSON.
|
void |
setRenderedAttributes(java.util.Set<java.lang.String> renderedAttributes)
已过时。
use
setModelKeys(Set) instead |
void |
setUpdateContentLength(boolean updateContentLength)
Whether to update the 'Content-Length' header of the response.
|
protected void |
writeContent(java.io.OutputStream stream,
java.lang.Object value,
java.lang.String jsonPrefix)
Write the actual JSON content to the stream.
|
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, toString, writeToResponsegetServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContextpublic static final java.lang.String DEFAULT_CONTENT_TYPE
AbstractView.setContentType(java.lang.String).public MappingJackson2JsonView()
MappingJackson2JsonView, setting the content type to application/json.public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
ObjectMapper for this view.
If not set, a default ObjectMapper will be used.
Setting a custom-configured ObjectMapper is one way to take further control of
the JSON serialization process. The other option is to use Jackson's provided annotations
on the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.
public final com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
ObjectMapper for this view.public void setEncoding(com.fasterxml.jackson.core.JsonEncoding encoding)
JsonEncoding for this view.
By default, UTF-8 is used.public final com.fasterxml.jackson.core.JsonEncoding getEncoding()
JsonEncoding for this view.public void setJsonPrefix(java.lang.String jsonPrefix)
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.
public void setPrettyPrint(boolean prettyPrint)
ObjectMapper as follows:
ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
The default value is false.
public void setModelKey(java.lang.String modelKey)
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 setDisableCaching(boolean disableCaching)
Default is true, which will prevent the client from caching the generated JSON.
public void setUpdateContentLength(boolean updateContentLength)
true, the response is buffered in order to determine the content
length and set the 'Content-Length' header of the response.
The default setting is false.
protected void prepareResponse(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
prepareResponse 在类中 org.springframework.web.servlet.view.AbstractViewprotected void renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws java.lang.Exception
renderMergedOutputModel 在类中 org.springframework.web.servlet.view.AbstractViewjava.lang.Exceptionprotected 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.
model - the model, as passed on to renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)protected void writeContent(java.io.OutputStream stream,
java.lang.Object value,
java.lang.String jsonPrefix)
throws java.io.IOException
stream - the output stream to usevalue - the value to be rendered, as returned from filterModel(java.util.Map<java.lang.String, java.lang.Object>)jsonPrefix - the prefix for this view's JSON output
(as indicated through setJsonPrefix(java.lang.String)/setPrefixJson(boolean))java.io.IOException - if writing failed