Top Description Fields Constructors Methods
org.springframework.web.servlet.view

public Class ContentNegotiatingViewResolver

extends WebApplicationObjectSupport
implements ViewResolver, Ordered, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.springframework.core.Ordered, org.springframework.web.servlet.ViewResolver
Imports
java.util.ArrayList, .Collection, .Collections, .LinkedHashSet, .List, .Locale, .Map, .Set, jakarta.servlet.ServletContext, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.beans.factory.BeanFactoryUtils, .InitializingBean, org.springframework.core.Ordered, org.springframework.core.annotation.AnnotationAwareOrderComparator, org.springframework.http.MediaType, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, .MimeTypeUtils, .StringUtils, org.springframework.web.HttpMediaTypeNotAcceptableException, org.springframework.web.accept.ContentNegotiationManager, .ContentNegotiationManagerFactoryBean, org.springframework.web.context.request.RequestAttributes, .RequestContextHolder, .ServletRequestAttributes, .ServletWebRequest, org.springframework.web.context.support.WebApplicationObjectSupport, org.springframework.web.servlet.HandlerMapping, .SmartView, .View, .ViewResolver

Implementation of ViewResolver that resolves a view based on the request file name or Accept header.

The ContentNegotiatingViewResolver does not resolve views itself, but delegates to other ViewResolvers. By default, these other view resolvers are picked up automatically from the application context, though they can also be set explicitly by using the viewResolvers property

Note

that in order for this view resolver to work properly, the order property needs to be set to a higher precedence than the others (the default is Ordered#HIGHEST_PRECEDENCE).

This view resolver uses the requested media type to select a suitable View for a request. The requested media type is determined through the configured ContentNegotiationManager. Once the requested media type has been determined, this resolver queries each delegate view resolver for a View and determines if the requested media type is compatible with the view's content type). The most compatible view is returned.

Additionally, this view resolver exposes the defaultViews property, allowing you to override the views provided by the view resolvers. Note that these default views are offered as candidates, and still need have the content type requested (via file extension, parameter, or Accept header, described above).

For example, if the request path is /view.html, this view resolver will look for a view that has the text/html content type (based on the html file extension). A request for /view with a text/html request Accept header has the same result.

Authors
Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
Since
3.0
See Also
ViewResolver, InternalResourceViewResolver, BeanNameViewResolver

Field Summary

Modifier and TypeField and Description
private final ContentNegotiationManagerFactoryBean
private ContentNegotiationManager
private List<View>
private static final View
private int
private boolean
private List<ViewResolver>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

private View
getBestView(List<View> candidateViews, List<MediaType> requestedMediaTypes, RequestAttributes attrs)

private List<View>
getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes)

public ContentNegotiationManager
getContentNegotiationManager()

Return the ContentNegotiationManager to use to determine requested media types.

public List<View>
protected List<MediaType>

Returns:

the list of media types requested, if any
getMediaTypes
(HttpServletRequest
the current servlet request
request
)

Determines the list of MediaType for the given HttpServletRequest.

private MediaType
getMostSpecificMediaType(MediaType acceptType, MediaType produceType)

Return the more specific of the acceptable and the producible media types with the q-value of the former.

public int
getOrder()

Implements org.springframework.core.Ordered.getOrder.

Get the order value of this object.

private List<MediaType>
public List<ViewResolver>
protected void
initServletContext(ServletContext
the ServletContext that this application object runs in (never null)
servletContext
)

Overrides org.springframework.web.context.support.WebApplicationObjectSupport.initServletContext.

Subclasses may override this for custom initialization based on the ServletContext that this application object runs in.

The default implementation is empty.

public boolean
isUseNotAcceptableStatusCode()

Whether to return HTTP Status 406 if no suitable is found.

public View
resolveViewName(String
name of the view to resolve
viewName
,
Locale
the Locale in which to resolve the view. ViewResolvers that support internationalization should respect this.
locale
)

Implements org.springframework.web.servlet.ViewResolver.resolveViewName.

Resolve the given view by name.

public void
setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)

Set the ContentNegotiationManager to use to determine requested media types.

public void
setDefaultViews(List<View> defaultViews)

Set the default views to use when a more specific view can not be obtained from the ViewResolver chain.

public void
setOrder(int order)

public void
setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode)

Indicate whether a 406 Not Acceptable status code should be returned if no suitable view can be found.

public void
setViewResolvers(List<ViewResolver> viewResolvers)

Sets the view resolvers to be wrapped by this view resolver.

Inherited from org.springframework.web.context.support.WebApplicationObjectSupport:
getServletContextgetTempDirgetWebApplicationContextinitApplicationContextisContextRequiredsetServletContext

Field Detail

cnmFactoryBeanback to summary
private final ContentNegotiationManagerFactoryBean cnmFactoryBean
contentNegotiationManagerback to summary
private ContentNegotiationManager contentNegotiationManager
Annotations
@Nullable
defaultViewsback to summary
private List<View> defaultViews
Annotations
@Nullable
NOT_ACCEPTABLE_VIEWback to summary
private static final View NOT_ACCEPTABLE_VIEW
orderback to summary
private int order
useNotAcceptableStatusCodeback to summary
private boolean useNotAcceptableStatusCode
viewResolversback to summary
private List<ViewResolver> viewResolvers
Annotations
@Nullable

Constructor Detail

ContentNegotiatingViewResolverback to summary
public ContentNegotiatingViewResolver()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

Annotations
@Override
getBestViewback to summary
private View getBestView(List<View> candidateViews, List<MediaType> requestedMediaTypes, RequestAttributes attrs)
Annotations
@Nullable
getCandidateViewsback to summary
private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes) throws Exception
getContentNegotiationManagerback to summary
public ContentNegotiationManager getContentNegotiationManager()

Return the ContentNegotiationManager to use to determine requested media types.

Annotations
@Nullable
Since
4.1.9
getDefaultViewsback to summary
public List<View> getDefaultViews()
getMediaTypesback to summary
protected List<MediaType> getMediaTypes(HttpServletRequest request)

Determines the list of MediaType for the given HttpServletRequest.

Parameters
request:HttpServletRequest

the current servlet request

Returns:List<MediaType>

the list of media types requested, if any

Annotations
@Nullable
getMostSpecificMediaTypeback to summary
private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType)

Return the more specific of the acceptable and the producible media types with the q-value of the former.

getOrderback to summary
public int getOrder()

Implements org.springframework.core.Ordered.getOrder.

Doc from org.springframework.core.Ordered.getOrder.

Get the order value of this object.

Higher values are interpreted as lower priority. As a consequence, the object with the lowest value has the highest priority (somewhat analogous to Servlet load-on-startup values).

Same order values will result in arbitrary sort positions for the affected objects.

Returns:int

the order value

Annotations
@Override
getProducibleMediaTypesback to summary
private List<MediaType> getProducibleMediaTypes(HttpServletRequest request)
Annotations
@SuppressWarnings:unchecked
getViewResolversback to summary
public List<ViewResolver> getViewResolvers()
initServletContextback to summary
protected void initServletContext(ServletContext servletContext)

Overrides org.springframework.web.context.support.WebApplicationObjectSupport.initServletContext.

Doc from org.springframework.web.context.support.WebApplicationObjectSupport.initServletContext.

Subclasses may override this for custom initialization based on the ServletContext that this application object runs in.

The default implementation is empty. Called by initApplicationContext(org.springframework.context.ApplicationContext) as well as setServletContext(jakarta.servlet.ServletContext).

Parameters
servletContext:ServletContext

the ServletContext that this application object runs in (never null)

Annotations
@Override
isUseNotAcceptableStatusCodeback to summary
public boolean isUseNotAcceptableStatusCode()

Whether to return HTTP Status 406 if no suitable is found.

resolveViewNameback to summary
public View resolveViewName(String viewName, Locale locale) throws Exception

Implements org.springframework.web.servlet.ViewResolver.resolveViewName.

Doc from org.springframework.web.servlet.ViewResolver.resolveViewName.

Resolve the given view by name.

Note

To allow for ViewResolver chaining, a ViewResolver should return null if a view with the given name is not defined in it. However, this is not required: Some ViewResolvers will always attempt to build View objects with the given name, unable to return null (rather throwing an exception when View creation failed).

Parameters
viewName:String

name of the view to resolve

locale:Locale

the Locale in which to resolve the view. ViewResolvers that support internationalization should respect this.

Returns:View

the View object, or null if not found (optional, to allow for ViewResolver chaining)

Annotations
@Override
@Nullable
Exceptions
Exception:
if the view cannot be resolved (typically in case of problems creating an actual View object)
setContentNegotiationManagerback to summary
public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)

Set the ContentNegotiationManager to use to determine requested media types.

If not set, ContentNegotiationManager's default constructor will be used, applying a org.springframework.web.accept.HeaderContentNegotiationStrategy.

See Also
ContentNegotiationManager#ContentNegotiationManager()
setDefaultViewsback to summary
public void setDefaultViews(List<View> defaultViews)

Set the default views to use when a more specific view can not be obtained from the ViewResolver chain.

setOrderback to summary
public void setOrder(int order)
setUseNotAcceptableStatusCodeback to summary
public void setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode)

Indicate whether a 406 Not Acceptable status code should be returned if no suitable view can be found.

Default is false, meaning that this view resolver returns null for resolveViewName(String, Locale) when an acceptable view cannot be found. This will allow for view resolvers chaining. When this property is set to true, resolveViewName(String, Locale) will respond with a view that sets the response status to 406 Not Acceptable instead.

setViewResolversback to summary
public void setViewResolvers(List<ViewResolver> viewResolvers)

Sets the view resolvers to be wrapped by this view resolver.

If this property is not set, view resolvers will be detected automatically.