Spring MVC JSON Response Data
Here you will see how to return JSON response in Spring MVC application.
- Project structure:
- pom.xml:
<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.javahonk</groupId> <artifactId>SpringMVCReturnJSON</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringMVCReturnJSON Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <SpringVersion>4.0.6.RELEASE</SpringVersion> <jackson.version>1.9.10</jackson.version> </properties> <dependencies> <!-- Spring dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${SpringVersion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${SpringVersion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${SpringVersion}</version> </dependency> <!-- Jackson JSON Mapper --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>${jackson.version}</version> </dependency> </dependencies> <build> <finalName>SpringMVCReturnJSON</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
- Person.java
package com.javahonk.controller; import javax.xml.bind.annotation.XmlAttribute; public class Person { private String firstName; private String lastName; @XmlAttribute public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } @XmlAttribute public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
- SpringMVCController.java
package com.javahonk.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class SpringMVCController { @RequestMapping(value="/helloWorld.web",method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("message", "Hello World!!!"); return "helloWorld"; } @RequestMapping(value="/springMVCReturnJSON.web", method=RequestMethod.GET, produces={"application/json"}) public @ResponseBody Person getPerson() { Person person = new Person(); person.setFirstName("Java"); person.setLastName("Honk"); return person; } }
- Web page output:
For more information please refer this spring tutorial
Download Project: SpringMVCReturnJSON