Spring 3 MVC and JSON Response

JSON stands for Javascript Object Notation, and is a good alternative of XML for data transfers these these days.
To know more about JSON visit http://www.json.org/

In this tutorial we will explore generation of JSON response in Spring MVC.

Tools Used

  1. Eclipse
  2. Tomcat
  3. Jackson 1.7.1
  4. Spring 3.x
  5. Maven

Project Structure

This is a maven based project. Go here to see how to install maven


Maven dependencies for the project.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<name>spring-mvc-json Maven Webapp</name>


		<!-- Jackson JSON Mapper -->

		<!-- Spring 3 dependencies -->




Model Class

class Employee{
	private int id;
	private String name;
	public Employee(int id, String name) {
		this.id = id;
		this.name = name;
	public int getId() {
		return id;
	public void setId(int id) {
		this.id = id;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;

Controller Class

Spring does automatic JSON Conversion when it finds that following conditions are met.

  1. Spring mvc has been configured to be annotation driven in the spring configs
  2. Jackson library is present in the classpath
  3. Return method is annotated with @ResponseBody
package com.bharatonjava.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping(value = "/json")
public class JsonOutputContoller {

	public @ResponseBody Employee generateJsonResponse(@PathVariable("name") String name){
		Employee e = new Employee(1001, name); 
		return e;

Spring Configuration

File spring-config.xml contains the spring configuration. We need to configure spring mvc to be annotation driven in order to use @ResponseBody annotation.

<beans xmlns="http://www.springframework.org/schema/beans"
	<context:component-scan base-package="com.bharatonjava.controllers" />
	<mvc:annotation-driven />


Hit following URL in your favourite browser to see the output.


Source Code


, ,

  1. #1 by Amila Silva on August 29, 2013 - 8:25 am

    This works fine, great thanks a lot.

  2. #2 by G Sunderam on October 1, 2013 - 9:26 pm

    Great post. Works fine

  3. #3 by Gaurav on September 10, 2014 - 10:27 am

    Simple & Nice post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: