JSON objects or Strings can be returned from Spring MVC controllers using some simple steps. Lets see some Spring MVC Json response examples in this post.
What is JSON?
JSON is a way of easily exchanging and storing data. JSON is all about key/value pairs.You can read more about JSON on Wiki.
The important points to remember for getting the JSON response are
1. Marking the mvc as annotation driven.
2. Adding Jackson dependency to pom.xml
3. Creating a model class that we need to send as JSON
4. Using the
@ResponseBody in the Controller return value.
5. Using the
produces = "application/json" in the
@RequestMapping
1. Dispatcher Servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<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.kscodes.sampleproject" /> <mvc:annotation-driven /> </beans> |
2. Jackson configuration in pom.xml
1 2 3 4 5 |
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.10</version> </dependency> |
3. Employee Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
package com.kscodes.sampleproject.model; public class Employee { private String id; private String firstName; private String lastName; private int age; private double salary; public Employee() { super(); } public Employee(String id, String firstName, String lastName, int age, double salary) { super(); this.id = id; this.firstName = firstName; this.lastName = lastName; this.age = age; this.salary = salary; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } |
4. The Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.kscodes.sampleproject.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.kscodes.sampleproject.model.Employee; @Controller @RequestMapping(value = "/json") public class JsonResponseController { @RequestMapping(value = "/employees", method = RequestMethod.GET, produces = "application/json") public @ResponseBody List<Employee> getAllEmployee() { // We will set some dummy data and send it as response List<Employee> employees = new ArrayList<Employee>(); Employee empl1 = new Employee("001", "John", "Doe", 25, 5000d); Employee empl2 = new Employee("001", "Steve", "Smith", 22, 4500d); Employee empl3 = new Employee("001", "Rob", "D", 27, 6000.50); employees.add(empl1); employees.add(empl2); employees.add(empl3); return employees; } } |