Spring 3, ContentNegotiatingViewResolver, is an interesting view resolver, which allow you to output a same resource (content or data) to different type of views like JSP, XML, RSS, JSON and etc. Put it simple, see following web requested URL, which will return in different views.

  1. http://www.mkyong.com/fruit/banana.rss , returned as RSS file.
  2. http://www.mkyong.com/fruit/banana.xml , returned as XML file.
  3. http://www.mkyong.com/fruit/banana.json , returned as JSON file.
  4. http://www.mkyong.com/fruit/banana, returned to your default view resolver.
This ContentNegotiatingViewResolver first determine “which view resolver should return by file extension”, if no view is match, then use the default view resolver. Read this Spring documentation to study how it works.

In this tutorial, we show you how to use ContentNegotiatingViewResolver. At the end of this tutorial, a same model will be returned in different views – XML, JSON, RSS and JSP, based on it’s requested file extension.

Technologies used :

  1. Spring 3.0.5.RELEASE
  2. Jackson 1.7.1
  3. Rome 1.0.0
  4. JDK 1.6
  5. Maven 3
  6. Eclipse 3.6
JAXB is bundled in JDK1.6, so, you don’t need to include it manually.

1. Project Dependency

Declares following dependencies in your Maven pom.xml file.



		<!-- Spring 3 dependencies -->



		<!-- Jackson JSON Mapper -->

		<!-- RSS -->



2. Model

A Pojo, annotated with JAXB annotation, so that it can output in XML file. Besides, later we use this model to display in different views.

package com.mkyong.common.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "fruit")
public class Fruit {

	String name;
	int quality;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

	public int getQuality() {
		return quality;

	public void setQuality(int quality) {
		this.quality = quality;

	public Fruit(String name, int quality) {
		this.name = name;
		this.quality = quality;

	public Fruit() {


3. JSON and XML View

To output JSON and XML views, you don’t need to do any extra works, Spring MVC will handle the conversion automatically. Read this Spring MVC and XML, and Spring MVC and JSON examples.

4. RSS View

To output RSS View, you need to extend AbstractRssFeedView. Read this Spring 3 MVC and RSS example to know how it works.

package com.mkyong.common.rss;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.feed.AbstractRssFeedView;
import com.mkyong.common.model.Fruit;
import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Content;
import com.sun.syndication.feed.rss.Item;

public class RssFeedView extends AbstractRssFeedView {

	protected void buildFeedMetadata(Map<String, Object> model, Channel feed,
		HttpServletRequest request) {
		feed.setTitle("Sample Title");
		feed.setDescription("Sample Description");
		super.buildFeedMetadata(model, feed, request);
	protected List<Item> buildFeedItems(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Fruit fruit = (Fruit) model.get("model");
		String msg = fruit.getName() + fruit.getQuality();
		List<Item> items = new ArrayList<Item>(1);
		Item item = new Item();
		Content content = new Content();
		return items;

5. JSP View

A JSP page to display the model data.

File : list.jsp

	<h1>Spring @MVC ContentNegotiatingViewResolver</h1>
	Fruit Name : ${model.name} <br />
	Fruit Quality : ${model.quality}

6. Controller

Spring controller, to generate a “fruit” model and return it.

package com.mkyong.common.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.mkyong.common.model.Fruit;

public class FruitController {

	@RequestMapping(value="{fruitName}", method = RequestMethod.GET)
	public String getFruit(@PathVariable String fruitName, ModelMap model) {

		Fruit fruit = new Fruit(fruitName, 1000);
		model.addAttribute("model", fruit);
		return "list";


7. ContentNegotiatingViewResolver example

The code should be self-explanatory. However, you have to define the “order” property, where lower value get higher priority. In this case, when a URL is requested, Spring MVC will use “ContentNegotiatingViewResolver” (order=1) to return a suitable view (based on file extension declared in “mediaTypes” property), if not match, then use “InternalResourceViewResolver” (order=2) to return a default JSP page.

<beans xmlns="http://www.springframework.org/schema/beans"

	<context:component-scan base-package="com.mkyong.common.controller" />

	<mvc:annotation-driven />

	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
	  <property name="order" value="1" />
	  <property name="mediaTypes">
		   <entry key="json" value="application/json" />
		   <entry key="xml" value="application/xml" />
		   <entry key="rss" value="application/rss+xml" />

	  <property name="defaultViews">
		  <!-- JSON View -->

		  <!-- RSS View -->
		  <bean class="com.mkyong.common.rss.RssFeedView" />

		  <!-- JAXB XML View -->
		  <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
				<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
				   <property name="classesToBeBound">
	  <property name="ignoreAcceptHeader" value="true" />


	<!-- If no extension matched, use JSP view -->
		<property name="order" value="2" />
		<property name="prefix">
		<property name="suffix">


8. Demo

Same model and display in different views, via ContentNegotiatingViewResolver.

http://localhost:8080/SpringMVC/fruit/banana.xml , display as XML file.

spring mvc and xml demo

http://localhost:8080/SpringMVC/fruit/banana.json , display as JSON file.

spring mvc and json demo

http://localhost:8080/SpringMVC/fruit/banana.rss , display as RSS file.

spring mvc and RSS demo

http://localhost:8080/SpringMVC/fruit/banana , display as JSP page.

spring mvc and JSP demo

Download Source Code