REST Project to return both JSON and XML Response using Spring ContentNegotiationManager | JavaInUse

REST Project to return both JSON and XML Response using Spring ContentNegotiationManager


In this chapter we will implement the content negotiation for Spring Rest Webservices. Till now we have seen two projects, one project returned the response as xml and the other as JSON. But now suppose we we want to expose a single method as a REST webservice and this should return either xml or JSON depending on some user parameter. This is implemented using ContentNegotiation parameter defined by the Spring Framework. In the request url itself we specify the return type we want using some parameter.

Lets Begin

We will create Eclipse Maven project as follows-

The pom.xml is as follows-
<project xmlns="" xmlns:xsi=""
	<name>employee-management-system Maven Webapp</name>

The web.xml is as follows. It has a filter to allow all URLs.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="" xmlns:xsi=""

	<!-- Configure the Disptcher Servlet -->
	<!-- Allow URLs with all extensions -->

Define the DispatcherServlet.xml to define the configuration for ContentNegotiation parameter.
<beans xmlns=""

	<context:component-scan base-package="com.javainuse.controller" />
	<context:component-scan base-package="com.javainuse.service" />
			content-negotiation-manager="contentNegotiationManager" />

	<bean id="contentNegotiationManager"

		<property name="favorPathExtension" value="false" />

		<property name="favorParameter" value="true" />

		<property name="parameterName" value="type" />

<!-- define the keys which will be specified in the URL to specify the return type -->
		<property name="mediaTypes">
				<entry key="json" value="application/json"></entry>
				<entry key="xml" value="application/xml" />

Define the Employee Model class as follows-
package com.javainuse.domain;

import javax.xml.bind.annotation.XmlRootElement;

public class Employee {
	private String empId;
	private String name;
	private String designation;
	private double salary;

	public Employee() {

	public Employee(String empId, String name, String designation, double salary) {
		this.setEmpId(empId); = name;
		this.designation = designation;
		this.salary = salary;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getDesignation() {
		return designation;

	public void setDesignation(String designation) {
		this.designation = designation;

	public double getSalary() {
		return salary;

	public void setSalary(double salary) {
		this.salary = salary;

	public String getEmpId() {
		return empId;

	public void setEmpId(String empId) {
		this.empId = empId;


Define the service layer and its implementation as follows- Also in the Dispatcher-context.xml we have context:component-scan base-package="com.javainuse.service" Spring scans this particular package for a class annotated with @Component. The EmployeeService class will actually fetch all the required Employee information. Usually EmployeeService should call the EmployeeDAO which will return all items from the DataBase.But here we will mock the EmployeeService class to just return the list of items as follows. We are using the Programming to Interface concept here. You can read more about this in this chapter. So we create the and as follows.
package com.javainuse.service;

import java.util.List;

import com.javainuse.domain.Employee;

public interface EmployeeService {
	public List<Employee> getAllEmployees();

package com.javainuse.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.javainuse.domain.Employee;

public class EmployeeServiceMockImpl implements EmployeeService {

	private List<Employee> testEmployees = new ArrayList<Employee>();

	// populate the Employee List
	public EmployeeServiceMockImpl() {
		testEmployees.add(new Employee("1", "emp1", "M1", 10000));
		testEmployees.add(new Employee("2", "emp2", "M2", 20000));
		testEmployees.add(new Employee("3", "emp3", "M3", 30000));
		testEmployees.add(new Employee("4", "emp4", "M4", 40000));
		testEmployees.add(new Employee("5", "emp5", "M5", 50000));
		testEmployees.add(new Employee("6", "emp6", "M6", 60000));

	// Return the Mocked Employee List
	public List<Employee> getAllEmployees() {
		return new ArrayList<Employee>(testEmployees);

No header information like headers="Accept=application/json" or headers="Accept=application/xml" is needed. Since the return type format is being specified by the ContentNegotiationManager in the configuration file.
package com.javainuse.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.javainuse.domain.Employee;
import com.javainuse.service.EmployeeService;

public class DisplayEmployeeController {

	private EmployeeService employeeService;

	@RequestMapping(value = "/")
	public Employee viewAllItems() {
		List<Employee> allEmployees = employeeService.getAllEmployees();
		return allEmployees.get(0);


These are the only changes required. We now run the two eclipse commands- clean install, tomcat:run to deploy the application.
Hit the browser with the following URL for json response

Hit the browser with the following URL for json response

Download Source Code

Download it - REST Project with ContentNegotiationManager

See Also

REST Project to return XML Response
REST Project to return JSON Response