Class AbstractJackson2View
- 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
- All Implemented Interfaces:
Aware,BeanNameAware,ApplicationContextAware,ServletContextAware,View
- Direct Known Subclasses:
MappingJackson2JsonView,MappingJackson2XmlView
public abstract class AbstractJackson2View extends AbstractView
Abstract base class for Jackson based and content type independentAbstractViewimplementations.Compatible with Jackson 2.6 and higher, as of Spring 4.3.
- Since:
- 4.1
- Author:
- Jeremy Grelle, Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
Field Summary
Fields Modifier and Type Field Description protected booleanupdateContentLengthFields inherited from class org.springframework.web.servlet.view.AbstractView
DEFAULT_CONTENT_TYPE
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
Fields inherited from interface org.springframework.web.servlet.View
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractJackson2View(ObjectMapper objectMapper, String contentType)
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected ObjectfilterAndWrapModel(Map<String,Object> model, HttpServletRequest request)Filter and optionally wrap the model inMappingJacksonValuecontainer.protected abstract ObjectfilterModel(Map<String,Object> model)Filter out undesired attributes from the given model.JsonEncodinggetEncoding()Return theJsonEncodingfor this view.ObjectMappergetObjectMapper()Return theObjectMapperfor this view.protected voidprepareResponse(HttpServletRequest request, HttpServletResponse response)Prepare the given response for rendering.protected voidrenderMergedOutputModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response)Subclasses must implement this method to actually render the view.voidsetDisableCaching(boolean disableCaching)Disables caching of the generated JSON.voidsetEncoding(JsonEncoding encoding)Set theJsonEncodingfor this view.abstract voidsetModelKey(String modelKey)Set the attribute in the model that should be rendered by this view.voidsetObjectMapper(ObjectMapper objectMapper)Set theObjectMapperfor this view.voidsetPrettyPrint(boolean prettyPrint)Whether to use the default pretty printer when writing the output.voidsetUpdateContentLength(boolean updateContentLength)Whether to update the 'Content-Length' header of the response.protected voidwriteContent(OutputStream stream, Object object)Write the actual JSON content to the stream.protected voidwritePrefix(JsonGenerator generator, Object object)Write a prefix before the main content.protected voidwriteSuffix(JsonGenerator generator, Object object)Write a suffix after the main content.Methods inherited from class 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, setResponseContentType, 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
Field Detail
updateContentLength
protected boolean updateContentLength
Constructor Detail
AbstractJackson2View
protected AbstractJackson2View(ObjectMapper objectMapper, String contentType)
Method Detail
setObjectMapper
public void setObjectMapper(ObjectMapper objectMapper)
Set theObjectMapperfor this view. If not set, a defaultObjectMapperwill be used.Setting a custom-configured
ObjectMapperis 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.
getObjectMapper
public final ObjectMapper getObjectMapper()
Return theObjectMapperfor this view.
setEncoding
public void setEncoding(JsonEncoding encoding)
Set theJsonEncodingfor this view. By default, UTF-8 is used.
getEncoding
public final JsonEncoding getEncoding()
Return theJsonEncodingfor this view.
setPrettyPrint
public void setPrettyPrint(boolean prettyPrint)
Whether to use the default pretty printer when writing the output. This is a shortcut for setting up anObjectMapperas follows:ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
The default value is
false.
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.
setUpdateContentLength
public void setUpdateContentLength(boolean updateContentLength)
Whether to update the 'Content-Length' header of the response. When set totrue, 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.
prepareResponse
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response)
Description copied from class:AbstractViewPrepare the given response for rendering.The default implementation applies a workaround for an IE bug when sending download content via HTTPS.
- Overrides:
prepareResponsein classAbstractView- Parameters:
request- current HTTP requestresponse- current HTTP response
renderMergedOutputModel
protected void renderMergedOutputModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception
Description copied from class:AbstractViewSubclasses 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:
renderMergedOutputModelin classAbstractView- Parameters:
model- combined output Map (nevernull), with dynamic values taking precedence over static attributesrequest- current HTTP requestresponse- current HTTP response- Throws:
Exception- if rendering failed
filterAndWrapModel
protected Object filterAndWrapModel(Map<String,Object> model, HttpServletRequest request)
Filter and optionally wrap the model inMappingJacksonValuecontainer.- Parameters:
model- the model, as passed on torenderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)request- current HTTP request- Returns:
- the wrapped or unwrapped value to be rendered
writeContent
protected void writeContent(OutputStream stream, Object object) throws IOException
Write the actual JSON content to the stream.- Parameters:
stream- the output stream to useobject- the value to be rendered, as returned fromfilterModel(java.util.Map<java.lang.String, java.lang.Object>)- Throws:
IOException- if writing failed
setModelKey
public abstract 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.
filterModel
protected abstract Object filterModel(Map<String,Object> model)
Filter out undesired attributes from the given model. The return value can be either anotherMapor a single value object.- Parameters:
model- the model, as passed on torenderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)- Returns:
- the value to be rendered
writePrefix
protected void writePrefix(JsonGenerator generator, Object object) throws IOException
Write a prefix before the main content.- Parameters:
generator- the generator to use for writing content.object- the object to write to the output message.- Throws:
IOException
writeSuffix
protected void writeSuffix(JsonGenerator generator, Object object) throws IOException
Write a suffix after the main content.- Parameters:
generator- the generator to use for writing content.object- the object to write to the output message.- Throws:
IOException