Class MappingJackson2JsonView
- All Implemented Interfaces:
Aware, BeanNameAware, ApplicationContextAware, ServletContextAware, View
View that renders JSON content by serializing the model for the current request
using Jackson 2'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).
The default constructor uses the default configuration provided by Jackson2ObjectMapperBuilder.
- Since:
- 3.1.2
- Author:
- Jeremy Grelle, Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDeprecated, for removal: This API element is subject to removal in a future version.Default content type: "application/json".Fields inherited from class AbstractJackson2View
updateContentLengthFields inherited from class ApplicationObjectSupport
loggerFields inherited from interface View
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated, for removal: This API element is subject to removal in a future version.Construct a newMappingJackson2JsonViewusing default configuration provided byJackson2ObjectMapperBuilderand setting the content type toapplication/json.MappingJackson2JsonView(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Deprecated, for removal: This API element is subject to removal in a future version.Construct a newMappingJackson2JsonViewusing the providedObjectMapperand setting the content type toapplication/json. -
Method Summary
Modifier and TypeMethodDescriptionprotected ObjectfilterModel(Map<String, Object> model) Deprecated, for removal: This API element is subject to removal in a future version.Filter out undesired attributes from the given model.Deprecated, for removal: This API element is subject to removal in a future version.Return the attributes in the model that should be rendered by this view.voidsetExtractValueFromSingleKeyModel(boolean extractValueFromSingleKeyModel) Deprecated, for removal: This API element is subject to removal in a future version.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.voidsetJsonPrefix(String jsonPrefix) Deprecated, for removal: This API element is subject to removal in a future version.Specify a custom prefix to use for this view's JSON output.voidsetModelKey(String modelKey) Deprecated, for removal: This API element is subject to removal in a future version.Set the attribute in the model that should be rendered by this view.voidsetModelKeys(@Nullable Set<String> modelKeys) Deprecated, for removal: This API element is subject to removal in a future version.Set the attributes in the model that should be rendered by this view.voidsetPrefixJson(boolean prefixJson) Deprecated, for removal: This API element is subject to removal in a future version.Indicates whether the JSON output by this view should be prefixed with")]}', ".protected voidwritePrefix(com.fasterxml.jackson.core.JsonGenerator generator, Object object) Deprecated, for removal: This API element is subject to removal in a future version.Write a prefix before the main content.Methods inherited from class AbstractJackson2View
filterAndWrapModel, getEncoding, getObjectMapper, prepareResponse, renderMergedOutputModel, setDisableCaching, setEncoding, setObjectMapper, setPrettyPrint, setUpdateContentLength, writeContent, writeSuffixMethods inherited from class AbstractView
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, formatViewName, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, toString, writeToResponseMethods inherited from class WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContextMethods inherited from class ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext
-
Field Details
-
DEFAULT_CONTENT_TYPE
Deprecated, for removal: This API element is subject to removal in a future version.Default content type: "application/json". Overridable throughAbstractView.setContentType(String).- See Also:
-
-
Constructor Details
-
MappingJackson2JsonView
public MappingJackson2JsonView()Deprecated, for removal: This API element is subject to removal in a future version.Construct a newMappingJackson2JsonViewusing default configuration provided byJackson2ObjectMapperBuilderand setting the content type toapplication/json. -
MappingJackson2JsonView
public MappingJackson2JsonView(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Deprecated, for removal: This API element is subject to removal in a future version.Construct a newMappingJackson2JsonViewusing the providedObjectMapperand setting the content type toapplication/json.- Since:
- 4.2.1
-
-
Method Details
-
setJsonPrefix
Deprecated, for removal: This API element is subject to removal in a future version.Specify a custom prefix to use for this view's JSON output. Default is none.- See Also:
-
setPrefixJson
public void setPrefixJson(boolean prefixJson) Deprecated, for removal: This API element is subject to removal in a future version.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.
- See Also:
-
setModelKey
Deprecated, for removal: This API element is subject to removal in a future version.Description copied from class:AbstractJackson2ViewSet the attribute in the model that should be rendered by this view. When set, all other model attributes will be ignored.- Specified by:
setModelKeyin classAbstractJackson2View
-
setModelKeys
-
getModelKeys
-
setExtractValueFromSingleKeyModel
public void setExtractValueFromSingleKeyModel(boolean extractValueFromSingleKeyModel) Deprecated, for removal: This API element is subject to removal in a future version.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
MappingJackson2HttpMessageConverterwith an@ResponseBodyrequest-handling method.Default is
false. -
filterModel
Deprecated, for removal: This API element is subject to removal in a future version.Filter out undesired attributes from the given model. The return value can be either anotherMapor a single value object.The default implementation removes
BindingResultinstances and entries not included in themodelKeysproperty.- Specified by:
filterModelin classAbstractJackson2View- Parameters:
model- the model, as passed on toAbstractJackson2View.renderMergedOutputModel(Map, HttpServletRequest, HttpServletResponse)- Returns:
- the value to be rendered
-
writePrefix
protected void writePrefix(com.fasterxml.jackson.core.JsonGenerator generator, Object object) throws IOException Deprecated, for removal: This API element is subject to removal in a future version.Description copied from class:AbstractJackson2ViewWrite a prefix before the main content.- Overrides:
writePrefixin classAbstractJackson2View- Parameters:
generator- the generator to use for writing content.object- the object to write to the output message.- Throws:
IOException
-
JacksonJsonView