Spring REST Hello World JSON Tutorial with Example

January 19, 2016 , , 0 Comments

In this tutorial, we will learn to write APIs capable of returning JSON representations of resources.

Step 1 :- Open Eclipse and Create Dynamic Web Project named SpringRESTExample

Step 2:- Make sure you use Target Runtime as Apache Tomcat 7.0 and Dynamic web module version as 3.0.

Step 3 :- copy below jars to WEB-INF/lib folder.
  • commons-logging-1.2.jar
  • jackson-annotations-2.6.0.jar
  • jackson-core-2.6.0.jar
  • jackson-databind-2.6.0.jar
  • spring-aop-4.1.4.RELEASE.jar
  • spring-beans-4.1.4.RELEASE.jar
  • spring-context-4.1.4.RELEASE.jar
  • spring-core-4.1.4.RELEASE.jar
  • spring-expression-4.1.4.RELEASE.jar
  • spring-web-4.1.4.RELEASE.jar
  • spring-webmvc-4.1.4.RELEASE.jar
STEP 4:- Create Spring Configuration Bean file. /WebContent/WEB-INF/dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 <context:component-scan base-package="com.tutorialsdesk.controller" />
 <bean name="viewResolver"
 <bean name="jsonTemplate"

You will need to configure viewName “jsonTemplate” as bean of type MappingJackson2JsonView. And you will need to configure view resolver of type BeanNameViewResolver. This way viewName “jsonTemplate” will be matched with MappingJackson2JsonView and parsed JSON response will be returned to client.

STEP 5:- Map Spring MVC in /WebContent/WEB-INF/web.xml file as below
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

Using @ResponseBody Annotation

This technique is simple and easy. You have to include only jackson dependencies into classpath of your application and spring will register Jackson2JsonMessageConverter class automatically into context. Whenever you ask for a resource from REST API and provide http header “accept: application/json“, you will get back the json representation of resource.

Here @RestController = @Controller + @ResponseBody

If you are using @RestController you don't need to specify @ResponseBody

STEP 6:- Create Controller Class
  • package com.tutorialsdesk.controller
  • Filename: EmployeeController.java
package com.tutorialsdesk.controller;

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

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.RestController;

import com.tutorialsdesk.model.Employee;

public class EmployeeController {
 @RequestMapping(value = "/Employees", method =
 public List<Employee> getCountries(){ 
 List<Employee> listOfEmployees = new ArrayList<Employee>(); 
 return listOfEmployees; 
 @RequestMapping(value = "/Employee/{id}", method =
 public Employee getEmployeeById(@PathVariable int id){ 
 List<Employee> listOfEmployees = new ArrayList<Employee>(); 
 for (Employee Employee: listOfEmployees) { 
 return Employee; 
 return null; 

 public List<Employee> createEmployeeList(){ 
 List<Employee> listOfEmployees = new ArrayList<Employee>(); 
 listOfEmployees.add(new Employee(29, "Prakash")); 
 listOfEmployees.add(new Employee(30, "Rambir")); 
 listOfEmployees.add(new Employee(35, "Sachin")); 
 listOfEmployees.add(new Employee(25, "Martin")); 
 return listOfEmployees; 

STEP 7:- Create Model Class
  • package com.tutorialsdesk.model
  • Filename: Employee.java
package com.tutorialsdesk.model;

public class Employee {

 private String name;
 private int empId;

 public String getName() {
 return name;

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

 public int getEmpId() {
 return empId;

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

 public Employee(int empId, String name){
 this.name = name;
 this.empId = empId;

STEP 8 :- Run your project enter below URL in your browser

Now when you hit the URL : http://localhost:8080/SpringRESTExample/rest/Employees you will get this result.

If you hit the URL : http://localhost:8080/SpringRESTExample/rest/Employee/25 you will get this result.

Keep visiting TutorialsDesk for more tutorials and practical programming examples on Spring MVC. Hope we are able to explain you Spring REST Hello World JSON Example, if you have any questions or suggestions please write to us using contact us form.

Please share us on social media if you like the tutorial.

Prakash Hari Sharma
I'm a software developer and Java enthusiast. I like clean and modular code, enjoy Agile projects and have a passion for trying out new things. I try to learn and teach every day, and occasionally I'm even able to blog about it. :)