Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Spring Spring3.2 ContentNegotiatingViewResolver

Related Tutorials


Share on Google+Share on Google+


In this section, you will learn about Spring MVC view resolver : ContentNegotiatingViewResolver.


In this section, you will learn about Spring MVC view resolver : ContentNegotiatingViewResolver.

Spring introduces ContentNegotiatingViewResolver to provide multiple representations of a resource. The ContentNegotiatingViewResolver seeks other view resolver's help for resolving view rather resolve view itself. It choose the view according to the requested representation. Spring introduces ContentNegotiatingViewResolver to resolve view based on Accept header of the HTTP request or file extension.

There are two strategy exists for requesting representation from the server :

  • Using a distinct file extension for each type of URI. For example: http://www, requesting PDF representation and http://www, requesting XML representation.

  • By setting  Accept HTTP request header to list the media types that it can translate. For example, a request for with an Accept header set to 'application/pdf' requests a PDF representation.

One problem with the Accept header is that it is not possible to set Accept header in a web browser within HTML.

Sample configuration of the ContentNegotiatingViewResolver is given below :

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
	<property name="mediaTypes">
			<entry key="atom" value="application/atom+xml"/>
			<entry key="html" value="text/html"/>
			<entry key="json" value="application/json"/>
	<property name="viewResolvers">
		<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/jsp/"/>
			<property name="suffix" value=".jsp"/>
	<property name="defaultViews">
			<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />

<bean id="format" class="net.roseindia.ExampleContentFormat"/>


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 10, 2013

Related Tutorials

Discuss: ContentNegotiatingViewResolver  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image