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

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

POM

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">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.bharatonjava</groupId>
	<artifactId>spring-mvc-json</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-mvc-json Maven Webapp</name>
	<url>http://maven.apache.org</url>
	
	<properties>
		<spring.version>3.0.5.RELEASE</spring.version>
	</properties>

	<dependencies>

		<!-- Jackson JSON Mapper -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.7.1</version>
		</dependency>

		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>spring-mvc-json</finalName>
	</build>
</project>

Model Class

class Employee{
	private int id;
	private String name;
	
	public Employee(int id, String name) {
		super();
		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;

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

	@RequestMapping("/emp/{name}")
	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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="

http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<context:component-scan base-package="com.bharatonjava.controllers" />
 
	<mvc:annotation-driven />
 
</beans>

Output

Hit following URL in your favourite browser to see the output.
http://localhost:8080/spring-mvc-json/json/emp/bharat

json-output

Source Code

Sourcecode

About these ads

, ,

  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

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

Follow

Get every new post delivered to your Inbox.

Join 50 other followers

%d bloggers like this: