spring-framework / org.springframework.web.servlet.view / ContentNegotiatingViewResolver

ContentNegotiatingViewResolver

open class ContentNegotiatingViewResolver : WebApplicationObjectSupport, ViewResolver, Ordered, InitializingBean

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 MediaType 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.

Author
Arjen Poutsma

Author
Juergen Hoeller

Author
Rossen Stoyanchev

Since
3.0

See Also
ViewResolverInternalResourceViewResolverBeanNameViewResolver

Constructors

<init>

ContentNegotiatingViewResolver()

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 MediaType 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.

Functions

afterPropertiesSet

open fun afterPropertiesSet(): Unit

getContentNegotiationManager

open fun getContentNegotiationManager(): ContentNegotiationManager

Return the ContentNegotiationManager to use to determine requested media types.

getDefaultViews

open fun getDefaultViews(): MutableList<View>

getOrder

open fun getOrder(): Int

getViewResolvers

open fun getViewResolvers(): MutableList<ViewResolver>

isUseNotAcceptableStatusCode

open fun isUseNotAcceptableStatusCode(): Boolean

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

resolveViewName

open fun resolveViewName(viewName: String, locale: Locale): View

setContentNegotiationManager

open fun setContentNegotiationManager(contentNegotiationManager: ContentNegotiationManager): Unit

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.

setDefaultViews

open fun setDefaultViews(defaultViews: MutableList<View>): Unit

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

setOrder

open fun setOrder(order: Int): Unit

setUseNotAcceptableStatusCode

open fun setUseNotAcceptableStatusCode(useNotAcceptableStatusCode: Boolean): Unit

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.

setViewResolvers

open fun setViewResolvers(viewResolvers: MutableList<ViewResolver>): Unit

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

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