In this tutorial, we show you how to output JSON data in Spring MVC framework.

Technologies used :

  1. Spring 3.2.2.RELEASE
  2. Jackson 1.9.10
  3. JDK 1.6
  4. Eclipse 3.6
  5. Maven 3

P.S In Spring 3, to output JSON data, just puts Jackson library in the project classpath.

1. Project Dependencies

Get Jackson and Spring dependencies.

<project xmlns="" 
	<name>SpringMVC Json Webapp</name>
		<!-- Spring 3 dependencies -->
		<!-- Jackson JSON Mapper -->

2. Model

A simple POJO, later output this object as formatted JSON data.

package com.mkyong.common.model;
public class Shop {
	String name;
	String staffName[];
	//getter and setter methods

3. Controller

Add @ResponseBody as return value. Wen Spring sees

  1. Jackson library is existed in the project classpath
  2. The mvc:annotation-driven is enabled
  3. Return method annotated with @ResponseBody

Spring will handle the JSON conversion automatically.
package com.mkyong.common.controller;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mkyong.common.model.Shop;
public class JSONController {
	@RequestMapping(value="{name}", method = RequestMethod.GET)
	public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
		Shop shop = new Shop();
		shop.setStaffName(new String[]{"mkyong1", "mkyong2"});
		return shop;

4. mvc:annotation-driven

Enable mvc:annotation-driven in your Spring configuration XML file.

<beans xmlns=""
	xsi:schemaLocation="    ">
	<context:component-scan base-package="com.mkyong.common.controller" />
	<mvc:annotation-driven />

5. Demo

URL : http://localhost:8080/SpringMVC/rest/kfc/brands/kfc-kampar

spring mvc and json demo

Download Source Code

Download it – (21 KB)


  1. mvc-annotation-driven documentation
  2. High-performance JSON processor
  3. Spring MVC and XML example
Tags :
Founder of, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts