Class JacksonJsonView
- All Implemented Interfaces:
- Aware,- BeanNameAware,- ApplicationContextAware,- ServletContextAware,- View
View that renders JSON content by serializing the model for the current request
 using Jackson 3'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 following special model entries are supported:
- A JSON view with a com.fasterxml.jackson.annotation.JsonViewkey and the class name of the JSON view as value.
- A filter provider with a tools.jackson.databind.ser.FilterProviderkey and the filter provider class name as value.
- Since:
- 7.0
- Author:
- Sebastien Deleuze
- 
Field SummaryFieldsFields inherited from class org.springframework.web.servlet.view.AbstractJacksonViewFILTER_PROVIDER_HINT, JSON_VIEW_HINT, updateContentLengthFields inherited from class org.springframework.context.support.ApplicationObjectSupportloggerFields inherited from interface org.springframework.web.servlet.ViewPATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
- 
Constructor SummaryConstructorsConstructorDescriptionConstruct a new instance with aJsonMappercustomized with theJacksonModules found byMapperBuilder.findModules(ClassLoader)and setting the content type toapplication/json.JacksonJsonView(ObjectMapper objectMapper) Construct a new instance using the providedObjectMapperand setting the content type to "application/json".
- 
Method SummaryModifier and TypeMethodDescriptionprotected ObjectfilterModel(Map<String, Object> model, HttpServletRequest request) Filter out undesired attributes from the given model.Return the attributes in the model that should be rendered by this view.voidsetExtractValueFromSingleKeyModel(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.voidsetJsonPrefix(String jsonPrefix) Specify a custom prefix to use for this view's JSON output.voidsetModelKey(String modelKey) Set the attribute in the model that should be rendered by this view.voidsetModelKeys(@Nullable Set<String> modelKeys) Set the attributes in the model that should be rendered by this view.voidsetPrefixJson(boolean prefixJson) Indicates whether the JSON output by this view should be prefixed with")]}', ".protected voidwritePrefix(tools.jackson.core.JsonGenerator generator, Object object) Write a prefix before the main content.Methods inherited from class org.springframework.web.servlet.view.AbstractJacksonViewgetEncoding, prepareResponse, renderMergedOutputModel, setDisableCaching, setEncoding, setUpdateContentLength, writeContent, writeSuffixMethods inherited from class org.springframework.web.servlet.view.AbstractViewaddStaticAttribute, 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 org.springframework.web.context.support.WebApplicationObjectSupportgetServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContextMethods inherited from class org.springframework.context.support.ApplicationObjectSupportgetApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext
- 
Field Details- 
DEFAULT_CONTENT_TYPEDefault content type: "application/json".Overridable through AbstractView.setContentType(String).- See Also:
 
 
- 
- 
Constructor Details- 
JacksonJsonViewpublic JacksonJsonView()Construct a new instance with aJsonMappercustomized with theJacksonModules found byMapperBuilder.findModules(ClassLoader)and setting the content type toapplication/json.
- 
JacksonJsonViewConstruct a new instance using the providedObjectMapperand setting the content type to "application/json".
 
- 
- 
Method Details- 
setJsonPrefixSpecify a custom prefix to use for this view's JSON output.Default is none. - See Also:
 
- 
setPrefixJsonpublic 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 should be stripped before parsing the string as JSON. - See Also:
 
- 
setModelKeyDescription copied from class:AbstractJacksonViewSet the attribute in the model that should be rendered by this view.When set, all other model attributes will be ignored. - Specified by:
- setModelKeyin class- AbstractJacksonView
 
- 
setModelKeys
- 
getModelKeys
- 
setExtractValueFromSingleKeyModelpublic 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.The effect of setting this flag is similar to using JacksonJsonHttpMessageConverterwith an@ResponseBodyrequest-handling method.Default is false.
- 
filterModelFilter out undesired attributes from the given model.The return value can be either another Mapor a single value object.The default implementation removes BindingResultinstances and entries not included in themodelKeysproperty.- Specified by:
- filterModelin class- AbstractJacksonView
- Parameters:
- model- the model, as passed on to- AbstractJacksonView.renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)
- request- current HTTP request
- Returns:
- the value to be rendered
 
- 
writePrefixprotected void writePrefix(tools.jackson.core.JsonGenerator generator, Object object) throws IOException Description copied from class:AbstractJacksonViewWrite a prefix before the main content.- Overrides:
- writePrefixin class- AbstractJacksonView
- Parameters:
- generator- the generator to use for writing content
- object- the object to write to the output message
- Throws:
- IOException
 
 
-