|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractView
org.springframework.web.servlet.view.json.MappingJacksonJsonView
public class MappingJacksonJsonView
Spring-MVC View
that renders JSON content by serializing the model for the current request using Jackson'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)
. Or you can select specific model attributes to be encoded
as JSON via ... TODO
MappingJacksonHttpMessageConverter
Field Summary | |
---|---|
static java.lang.String |
DEFAULT_CONTENT_TYPE
Default content type. |
private boolean |
disableCaching
|
private JsonEncoding |
encoding
|
private boolean |
extractValueFromSingleKeyModel
|
private java.util.Set<java.lang.String> |
modelKeys
|
private ObjectMapper |
objectMapper
|
private boolean |
prefixJson
|
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport |
---|
logger |
Fields inherited from interface org.springframework.web.servlet.View |
---|
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE |
Constructor Summary | |
---|---|
MappingJacksonJsonView()
Construct a new JacksonJsonView , setting the content type to application/json . |
Method Summary | |
---|---|
protected java.lang.Object |
filterModel(java.util.Map<java.lang.String,java.lang.Object> model)
Filters out undesired attributes from the given model. |
java.util.Set<java.lang.String> |
getModelKeys()
Returns the attributes in the model that should be rendered by this view. |
java.util.Set<java.lang.String> |
getRenderedAttributes()
Deprecated. use getModelKeys() instead |
protected void |
prepareResponse(HttpServletRequest request,
HttpServletResponse response)
Prepare the given response for rendering. |
protected void |
renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request,
HttpServletResponse response)
Subclasses must implement this method to actually render the view. |
void |
setDisableCaching(boolean disableCaching)
Disables caching of the generated JSON. |
void |
setEncoding(JsonEncoding encoding)
Sets the JsonEncoding for this converter. |
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 |
setModelKey(java.lang.String modelKey)
Sets the attribute in the model that should be rendered by this view. |
void |
setModelKeys(java.util.Set<java.lang.String> modelKeys)
Sets the attributes in the model that should be rendered by this view. |
void |
setObjectMapper(ObjectMapper objectMapper)
Sets the ObjectMapper for 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 |
Methods inherited from class org.springframework.web.servlet.view.AbstractView |
---|
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, isExposePathVariables, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposePathVariables, setRequestContextAttribute, 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, requiredContextClass, setApplicationContext |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String DEFAULT_CONTENT_TYPE
private ObjectMapper objectMapper
private JsonEncoding encoding
private boolean prefixJson
private java.util.Set<java.lang.String> modelKeys
private boolean extractValueFromSingleKeyModel
private boolean disableCaching
Constructor Detail |
---|
public MappingJacksonJsonView()
JacksonJsonView
, setting the content type to application/json
.
Method Detail |
---|
public void setObjectMapper(ObjectMapper objectMapper)
ObjectMapper
for this view. If not set, a default ObjectMapper
is used.
Setting a custom-configured ObjectMapper
is one way to take further control of the JSON serialization
process. For example, an extended SerializerFactory
can be configured that provides custom serializers for
specific types. The other option for refining the serialization process is to use Jackson's provided annotations on
the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.
public void setEncoding(JsonEncoding encoding)
JsonEncoding
for this converter. By default, UTF-8 is used.
public void setPrefixJson(boolean prefixJson)
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 setModelKey(java.lang.String modelKey)
public void setModelKeys(java.util.Set<java.lang.String> modelKeys)
public java.util.Set<java.lang.String> getModelKeys()
public void setRenderedAttributes(java.util.Set<java.lang.String> renderedAttributes)
setModelKeys(Set)
instead
public java.util.Set<java.lang.String> getRenderedAttributes()
getModelKeys()
instead
public void setDisableCaching(boolean disableCaching)
Default is true
, which will prevent the client from caching the generated JSON.
public void setExtractValueFromSingleKeyModel(boolean extractValueFromSingleKeyModel)
MappingJacksonHttpMessageConverter
with an @ResponseBody
.
request-handling method.
Default is false
.
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response)
AbstractView
The default implementation applies a workaround for an IE bug when sending download content via HTTPS.
prepareResponse
in class AbstractView
request
- current HTTP requestresponse
- current HTTP responseprotected void renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
AbstractView
The first step will be preparing the request: In the JSP case, this would mean setting model objects as request attributes. The second step will be the actual rendering of the view, for example including the JSP via a RequestDispatcher.
renderMergedOutputModel
in class AbstractView
model
- combined output Map (never null
),
with dynamic values taking precedence over static attributesrequest
- current HTTP requestresponse
- current HTTP response
java.lang.Exception
- if rendering failedprotected java.lang.Object filterModel(java.util.Map<java.lang.String,java.lang.Object> model)
Map
, or a
single value object.
Default implementation removes BindingResult
instances and entries not included in the renderedAttributes
property.
model
- the model, as passed on to renderMergedOutputModel(java.util.Map, HttpServletRequest, HttpServletResponse)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |