类 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
- 所有已实现的接口:
Aware
,BeanNameAware
,ApplicationContextAware
,ServletContextAware
,View
public class MappingJackson2JsonView extends AbstractJackson2View
Spring MVCView
that renders JSON content by serializing the model for the current request using Jackson 2'sObjectMapper
.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.6 and higher, as of Spring 4.3.
- 从以下版本开始:
- 3.1.2
- 作者:
- Jeremy Grelle, Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
字段概要
字段 修饰符和类型 字段 说明 static String
DEFAULT_CONTENT_TYPE
Default content type: "application/json".static String
DEFAULT_JSONP_CONTENT_TYPE
已过时。Will be removed as of Spring Framework 5.1, use CORS instead.从类继承的字段 org.springframework.web.servlet.view.json.AbstractJackson2View
updateContentLength
从类继承的字段 org.springframework.context.support.ApplicationObjectSupport
logger
从接口继承的字段 org.springframework.web.servlet.View
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
构造器概要
构造器 构造器 说明 MappingJackson2JsonView()
Construct a newMappingJackson2JsonView
using default configuration provided byJackson2ObjectMapperBuilder
and setting the content type toapplication/json
.MappingJackson2JsonView(ObjectMapper objectMapper)
Construct a newMappingJackson2JsonView
using the providedObjectMapper
and setting the content type toapplication/json
.
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 protected Object
filterAndWrapModel(Map<String,Object> model, HttpServletRequest request)
Filter and optionally wrap the model inMappingJacksonValue
container.protected Object
filterModel(Map<String,Object> model)
Filter out undesired attributes from the given model.Set<String>
getModelKeys()
Return the attributes in the model that should be rendered by this view.protected boolean
isValidJsonpQueryParam(String value)
已过时。Will be removed as of Spring Framework 5.1, use CORS 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(Set<String> jsonpParameterNames)
已过时。Will be removed as of Spring Framework 5.1, use CORS instead.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
setResponseContentType(HttpServletRequest request, HttpServletResponse response)
Set the content type of the response to the configuredcontent type
unless theView.SELECTED_CONTENT_TYPE
request attribute is present and set to a concrete media type.protected void
writePrefix(JsonGenerator generator, Object object)
Write a prefix before the main content.protected void
writeSuffix(JsonGenerator generator, Object object)
Write a suffix after the main content.从类继承的方法 org.springframework.web.servlet.view.json.AbstractJackson2View
getEncoding, getObjectMapper, prepareResponse, renderMergedOutputModel, setDisableCaching, setEncoding, setObjectMapper, setPrettyPrint, setUpdateContentLength, writeContent
从类继承的方法 org.springframework.web.servlet.view.AbstractView
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
从类继承的方法 org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
从类继承的方法 org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
字段详细资料
DEFAULT_CONTENT_TYPE
public static final String DEFAULT_CONTENT_TYPE
Default content type: "application/json". Overridable throughAbstractView.setContentType(java.lang.String)
.- 另请参阅:
- 常量字段值
DEFAULT_JSONP_CONTENT_TYPE
@Deprecated public static final String DEFAULT_JSONP_CONTENT_TYPE
已过时。Will be removed as of Spring Framework 5.1, use CORS instead.Default content type for JSONP: "application/javascript".- 另请参阅:
- 常量字段值
构造器详细资料
MappingJackson2JsonView
public MappingJackson2JsonView()
Construct a newMappingJackson2JsonView
using default configuration provided byJackson2ObjectMapperBuilder
and setting the content type toapplication/json
.
MappingJackson2JsonView
public MappingJackson2JsonView(ObjectMapper objectMapper)
Construct a newMappingJackson2JsonView
using the providedObjectMapper
and setting the content type toapplication/json
.- 从以下版本开始:
- 4.2.1
方法详细资料
setJsonPrefix
public void setJsonPrefix(String jsonPrefix)
Specify a custom prefix to use for this view's JSON output. Default is none.
setPrefixJson
public void setPrefixJson(boolean prefixJson)
Indicates whether the JSON output by this view should be prefixed with ")]}', ". Default isfalse
.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.
setModelKey
public void setModelKey(String modelKey)
Set the attribute in the model that should be rendered by this view. When set, all other model attributes will be ignored.- 指定者:
setModelKey
在类中AbstractJackson2View
setModelKeys
public void setModelKeys(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
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
.
setJsonpParameterNames
@Deprecated public void setJsonpParameterNames(Set<String> jsonpParameterNames)
已过时。Will be removed as of Spring Framework 5.1, use CORS instead.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".
- 从以下版本开始:
- 4.1
- 另请参阅:
- JSONP Wikipedia article
isValidJsonpQueryParam
@Deprecated protected boolean isValidJsonpQueryParam(String value)
已过时。Will be removed as of Spring Framework 5.1, use CORS instead.Validate the jsonp query parameter value. The default implementation returns true if it consists of digits, letters, or "_" and ".". Invalid parameter values are ignored.- 参数:
value
- the query param value, nevernull
- 从以下版本开始:
- 4.1.8
filterModel
protected Object filterModel(Map<String,Object> model)
Filter out undesired attributes from the given model. The return value can be either anotherMap
or a single value object.The default implementation removes
BindingResult
instances and entries not included in themodelKeys
property.- 指定者:
filterModel
在类中AbstractJackson2View
- 参数:
model
- the model, as passed on toAbstractJackson2View.renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
- 返回:
- the value to be rendered
filterAndWrapModel
protected Object filterAndWrapModel(Map<String,Object> model, HttpServletRequest request)
从类复制的说明:AbstractJackson2View
Filter and optionally wrap the model inMappingJacksonValue
container.- 覆盖:
filterAndWrapModel
在类中AbstractJackson2View
- 参数:
model
- the model, as passed on toAbstractJackson2View.renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
request
- current HTTP request- 返回:
- the wrapped or unwrapped value to be rendered
writePrefix
protected void writePrefix(JsonGenerator generator, Object object) throws IOException
从类复制的说明:AbstractJackson2View
Write a prefix before the main content.- 覆盖:
writePrefix
在类中AbstractJackson2View
- 参数:
generator
- the generator to use for writing content.object
- the object to write to the output message.- 抛出:
IOException
writeSuffix
protected void writeSuffix(JsonGenerator generator, Object object) throws IOException
从类复制的说明:AbstractJackson2View
Write a suffix after the main content.- 覆盖:
writeSuffix
在类中AbstractJackson2View
- 参数:
generator
- the generator to use for writing content.object
- the object to write to the output message.- 抛出:
IOException
setResponseContentType
protected void setResponseContentType(HttpServletRequest request, HttpServletResponse response)
从类复制的说明:AbstractView
Set the content type of the response to the configuredcontent type
unless theView.SELECTED_CONTENT_TYPE
request attribute is present and set to a concrete media type.