Package org.springframework.web.servlet.view

# Class ContentNegotiatingViewResolver

java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.ContentNegotiatingViewResolver

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.context.ApplicationContextAware`, `org.springframework.core.Ordered`, `org.springframework.web.context.ServletContextAware`, `ViewResolver`

public class ContentNegotiatingViewResolver extends org.springframework.web.context.support.WebApplicationObjectSupport implements ViewResolver, org.springframework.core.Ordered, org.springframework.beans.factory.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 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.

Since:
  3.0
Author:
  Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
See Also:
  - `ViewResolver`
  - `InternalResourceViewResolver`
  - `BeanNameViewResolver`

## Field Summary

### Fields inherited from class org.springframework.context.support.ApplicationObjectSupport

`logger`

### Fields inherited from interface org.springframework.core.Ordered

`HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE`

## Constructor Summary

Constructors
Constructor
Description
`ContentNegotiatingViewResolver()`

## Method Summary

Modifier and Type
Method
Description
`void`
`afterPropertiesSet()`
`org.springframework.web.accept.ContentNegotiationManager`
`getContentNegotiationManager()`
Return the `ContentNegotiationManager` to use to determine requested media types.
`List<View>`
`getDefaultViews()`
`protected List<org.springframework.http.MediaType>`
`getMediaTypes(HttpServletRequest request)`
Determines the list of `MediaType` for the given `HttpServletRequest`.
`int`
`getOrder()`
`List<ViewResolver>`
`getViewResolvers()`
`protected void`
`initServletContext(ServletContext servletContext)`
`boolean`
`isUseNotAcceptableStatusCode()`
Whether to return HTTP Status 406 if no suitable is found.
`View`
`resolveViewName(String viewName, Locale locale)`
Resolve the given view by name.
`void`
`setContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager)`
Set the `ContentNegotiationManager` to use to determine requested media types.
`void`
`setDefaultViews(List<View> defaultViews)`
Set the default views to use when a more specific view can not be obtained from the `ViewResolver` chain.
`void`
`setOrder(int order)`
`void`
`setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode)`
Indicate whether a `406 Not Acceptable` status code should be returned if no suitable view can be found.
`void`
`setViewResolvers(List<ViewResolver> viewResolvers)`
Sets the view resolvers to be wrapped by this view resolver.

### Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport

`getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, isContextRequired, setServletContext`

### Methods inherited from class org.springframework.context.support.ApplicationObjectSupport

`getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ContentNegotiatingViewResolver

public ContentNegotiatingViewResolver()

## Method Details

### setContentNegotiationManager

public void setContentNegotiationManager(@Nullable org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager)
Set the `ContentNegotiationManager` to use to determine requested media types.

If not set, ContentNegotiationManager's default constructor will be used, applying a `HeaderContentNegotiationStrategy`.

See Also:
  - `ContentNegotiationManager()`

### getContentNegotiationManager

@Nullable public org.springframework.web.accept.ContentNegotiationManager getContentNegotiationManager()
Return the `ContentNegotiationManager` to use to determine requested media types.

Since:
  4.1.9

### setUseNotAcceptableStatusCode

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.

### isUseNotAcceptableStatusCode

public boolean isUseNotAcceptableStatusCode()
Whether to return HTTP Status 406 if no suitable is found.

### setDefaultViews

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.

### getDefaultViews

public List<View> getDefaultViews()

### setViewResolvers

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.

### getViewResolvers

public List<ViewResolver> getViewResolvers()

### setOrder

public void setOrder(int order)

### getOrder

public int getOrder()

Specified by:
  `getOrder` in interface `org.springframework.core.Ordered`

### initServletContext

protected void initServletContext(ServletContext servletContext)

Overrides:
  `initServletContext` in class `org.springframework.web.context.support.WebApplicationObjectSupport`

### afterPropertiesSet

public void afterPropertiesSet()

Specified by:
  `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`

### resolveViewName

@Nullable public View resolveViewName(String viewName, Locale locale) throws Exception
Description copied from interface: `ViewResolver`
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).

Specified by:
  `resolveViewName` in interface `ViewResolver`
Parameters:
  `viewName` - name of the view to resolve
  `locale` - the Locale in which to resolve the view. ViewResolvers that support internationalization should respect this.
Returns:
  the View object, or `null` if not found (optional, to allow for ViewResolver chaining)
Throws:
  `Exception` - if the view cannot be resolved (typically in case of problems creating an actual View object)

### getMediaTypes

@Nullable protected List<org.springframework.http.MediaType> getMediaTypes(HttpServletRequest request)
Determines the list of `MediaType` for the given `HttpServletRequest`.

Parameters:
  `request` - the current servlet request
Returns:
  the list of media types requested, if any