public abstract class AbstractJackson2View extends AbstractView
AbstractView
implementations.
Compatible with Jackson 2.6 and higher, as of Spring 4.3.
Modifier and Type | Field and Description |
---|---|
private boolean |
disableCaching |
private JsonEncoding |
encoding |
private ObjectMapper |
objectMapper |
private java.lang.Boolean |
prettyPrint |
protected boolean |
updateContentLength |
DEFAULT_CONTENT_TYPE
logger
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
Modifier | Constructor and Description |
---|---|
protected |
AbstractJackson2View(ObjectMapper objectMapper,
java.lang.String contentType) |
Modifier and Type | Method and Description |
---|---|
private void |
configurePrettyPrint() |
protected java.lang.Object |
filterAndWrapModel(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request)
Filter and optionally wrap the model in
MappingJacksonValue container. |
protected abstract java.lang.Object |
filterModel(java.util.Map<java.lang.String,java.lang.Object> model)
Filter out undesired attributes from the given model.
|
JsonEncoding |
getEncoding()
Return the
JsonEncoding for this view. |
ObjectMapper |
getObjectMapper()
Return the
ObjectMapper for this view. |
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)
Set the
JsonEncoding for this view. |
abstract void |
setModelKey(java.lang.String modelKey)
Set the attribute in the model that should be rendered by this view.
|
void |
setObjectMapper(ObjectMapper objectMapper)
Set the
ObjectMapper for this view. |
void |
setPrettyPrint(boolean prettyPrint)
Whether to use the default pretty printer when writing the output.
|
void |
setUpdateContentLength(boolean updateContentLength)
Whether to update the 'Content-Length' header of the response.
|
protected void |
writeContent(java.io.OutputStream stream,
java.lang.Object object)
Write the actual JSON content to the stream.
|
protected void |
writePrefix(JsonGenerator generator,
java.lang.Object object)
Write a prefix before the main content.
|
protected void |
writeSuffix(JsonGenerator generator,
java.lang.Object object)
Write a suffix after the main content.
|
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
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
private ObjectMapper objectMapper
private JsonEncoding encoding
private java.lang.Boolean prettyPrint
private boolean disableCaching
protected boolean updateContentLength
protected AbstractJackson2View(ObjectMapper objectMapper, java.lang.String contentType)
public void setObjectMapper(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 ObjectMapper getObjectMapper()
ObjectMapper
for this view.public void setEncoding(JsonEncoding encoding)
JsonEncoding
for this view.
By default, UTF-8 is used.public final JsonEncoding getEncoding()
JsonEncoding
for this view.public void setPrettyPrint(boolean prettyPrint)
ObjectMapper
as follows:
ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
The default value is false
.
private void configurePrettyPrint()
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(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 responsejava.lang.Exception
- if rendering failedprotected java.lang.Object filterAndWrapModel(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request)
MappingJacksonValue
container.model
- the model, as passed on to renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, HttpServletRequest, HttpServletResponse)
request
- current HTTP requestprotected void writeContent(java.io.OutputStream stream, java.lang.Object object) throws java.io.IOException
stream
- the output stream to useobject
- the value to be rendered, as returned from filterModel(java.util.Map<java.lang.String, java.lang.Object>)
java.io.IOException
- if writing failedpublic abstract void setModelKey(java.lang.String modelKey)
protected abstract java.lang.Object filterModel(java.util.Map<java.lang.String,java.lang.Object> model)
Map
or a single value object.model
- the model, as passed on to renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, HttpServletRequest, HttpServletResponse)
protected void writePrefix(JsonGenerator generator, java.lang.Object object) throws java.io.IOException
generator
- the generator to use for writing content.object
- the object to write to the output message.java.io.IOException
protected void writeSuffix(JsonGenerator generator, java.lang.Object object) throws java.io.IOException
generator
- the generator to use for writing content.object
- the object to write to the output message.java.io.IOException