SUMMARY:
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.MappingJacksonJsonView
- All Implemented Interfaces:
- BeanNameAware, ApplicationContextAware, ServletContextAware, View
public class MappingJacksonJsonView - extends AbstractView
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. For cases where the contents of the map need to be filtered, users may specify a specific set of
model attributes to encode via the renderedAttributes property.
- Since:
- 3.0
- Author:
- Jeremy Grelle, Arjen Poutsma
- See Also:
MappingJacksonHttpMessageConverter
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> |
getRenderedAttributes()
Returns the attributes in the model that should be rendered by this view. |
protected void |
prepareResponse(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Prepare the given response for rendering. |
protected void |
renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Subclasses must implement this method to actually render the view. |
void |
setDisableCaching(boolean disableCaching)
Disables caching of the generated JSON. |
void |
setEncoding(org.codehaus.jackson.JsonEncoding encoding)
Sets the JsonEncoding for this converter. |
void |
setObjectMapper(org.codehaus.jackson.map.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)
Sets the attributes in the model that should be rendered by this view. |
Methods inherited from class org.springframework.web.servlet.view.AbstractView |
addStaticAttribute, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setRequestContextAttribute, toString, writeToResponse |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
DEFAULT_CONTENT_TYPE
public static final java.lang.String DEFAULT_CONTENT_TYPE
- Default content type. Overridable as bean property.
- See Also:
- Constant Field Values
objectMapper
private org.codehaus.jackson.map.ObjectMapper objectMapper
encoding
private org.codehaus.jackson.JsonEncoding encoding
prefixJson
private boolean prefixJson
renderedAttributes
private java.util.Set<java.lang.String> renderedAttributes
disableCaching
private boolean disableCaching
MappingJacksonJsonView
public MappingJacksonJsonView()
- Construct a new
JacksonJsonView , setting the content type to application/json .
setObjectMapper
public void setObjectMapper(org.codehaus.jackson.map.ObjectMapper objectMapper)
- Sets the
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.
setEncoding
public void setEncoding(org.codehaus.jackson.JsonEncoding encoding)
- Sets the
JsonEncoding for this converter. By default, UTF-8 is used.
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 does not affect the evaluation of JSON,
but if JSON validation is performed on the string, the prefix would need to be ignored.
getRenderedAttributes
public java.util.Set<java.lang.String> getRenderedAttributes()
- Returns the attributes in the model that should be rendered by this view.
setRenderedAttributes
public void setRenderedAttributes(java.util.Set<java.lang.String> renderedAttributes)
- Sets the attributes in the model that should be rendered by this view. When set, all other model attributes will be
ignored.
setDisableCaching
public void setDisableCaching(boolean disableCaching)
- Disables caching of the generated JSON.
Default is true , which will prevent the client from caching the generated JSON.
prepareResponse
protected void prepareResponse(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
- Description copied from class:
AbstractView
- Prepare the given response for rendering.
The default implementation applies a workaround for an IE bug
when sending download content via HTTPS.
- Overrides:
prepareResponse in class AbstractView
- Parameters:
request - current HTTP requestresponse - current HTTP response
renderMergedOutputModel
protected 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
- Description copied from class:
AbstractView
- Subclasses must implement this method to actually render the view.
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.
- Specified by:
renderMergedOutputModel in class AbstractView
- Parameters:
model - combined output Map (never null ),
with dynamic values taking precedence over static attributesrequest - current HTTP requestresponse - current HTTP response
- Throws:
java.lang.Exception - if rendering failed
filterModel
protected java.lang.Object filterModel(java.util.Map<java.lang.String,java.lang.Object> model)
- Filters out undesired attributes from the given model. The return value can be either another
Map , or a
single value object.
Default implementation removes BindingResult instances and entries not included in the renderedAttributes property.
- Parameters:
model - the model, as passed on to renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
- Returns:
- the object to be rendered
|