Tuesday, December 29, 2015

Spring 3 MVC and JSON example

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

Create a dynamic Web project as below.




You have to have the below jars in your lib folder.

commons-logging-1.2.jar
jackson-core-asl-1.9.7.jar
jackson-mapper-asl-1.9.7.jar
spring-aop-3.2.2jar
spring-aop-3.2.2.RELEASE-sources.jar
spring-aop-3.2.2.RELEASE.jar
spring-aspects-3.2.2.RELEASE-javadoc.jar
spring-aspects-3.2.2.RELEASE-sources.jar
spring-aspects-3.2.2.RELEASE.jar
spring-beans-3.2.2.RELEASE-javadoc.jar
spring-beans-3.2.2.RELEASE-sources.jar
spring-beans-3.2.2.RELEASE.jar
spring-build-src-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE-javadoc.jar
spring-context-3.2.2.RELEASE-sources.jar
spring-context-3.2.2.RELEASE.jar
spring-context-support-3.2.2.RELEASE-javadoc.jar
spring-context-support-3.2.2.RELEASE-sources.jar
spring-context-support-3.2.2.RELEASE.jar
spring-core-3.2.2.RELEASE-javadoc.jar
spring-core-3.2.2.RELEASE-sources.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE-javadoc.jar
spring-expression-3.2.2.RELEASE-sources.jar
spring-expression-3.2.2.RELEASE.jar
spring-instrument-3.2.2.RELEASE-javadoc.jar
spring-instrument-3.2.2.RELEASE-sources.jar
spring-instrument-3.2.2.RELEASE.jar
spring-instrument-tomcat-3.2.2.RELEASE-javadoc.jar
spring-instrument-tomcat-3.2.2.RELEASE-sources.jar
spring-instrument-tomcat-3.2.2.RELEASE.jar
spring-jdbc-3.2.2.RELEASE-javadoc.jar
spring-jdbc-3.2.2.RELEASE-sources.jar
spring-jdbc-3.2.2.RELEASE.jar
spring-jms-3.2.2.RELEASE-javadoc.jar
spring-jms-3.2.2.RELEASE-sources.jar
spring-jms-3.2.2.RELEASE.jar
spring-orm-3.2.2.RELEASE-javadoc.jar
spring-orm-3.2.2.RELEASE-sources.jar
spring-orm-3.2.2.RELEASE.jar
spring-oxm-3.2.2.RELEASE-javadoc.jar
spring-oxm-3.2.2.RELEASE-sources.jar
spring-oxm-3.2.2.RELEASE.jar
spring-struts-3.2.2.RELEASE-javadoc.jar
spring-struts-3.2.2.RELEASE-sources.jar
spring-struts-3.2.2.RELEASE.jar
spring-test-3.2.2.RELEASE-javadoc.jar
spring-test-3.2.2.RELEASE-sources.jar
spring-test-3.2.2.RELEASE.jar
spring-tx-3.2.2.RELEASE-javadoc.jar
spring-tx-3.2.2.RELEASE-sources.jar
spring-tx-3.2.2.RELEASE.jar
spring-web-3.2.2.RELEASE-javadoc.jar
spring-web-3.2.2.RELEASE-sources.jar
spring-web-3.2.2.RELEASE.jar
spring-webmvc-3.2.2.RELEASE-javadoc.jar
spring-webmvc-3.2.2.RELEASE-sources.jar
spring-webmvc-3.2.2.RELEASE.jar
spring-webmvc-portlet-3.2.2.RELEASE-javadoc.jar
spring-webmvc-portlet-3.2.2.RELEASE-sources.jar

spring-webmvc-portlet-3.2.2.RELEASE.jar

Below are the details of the files.

JSONController.java

package com.rajesh.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.rajesh.common.model.Shop;

@Controller
@RequestMapping("/sample/words")
public class JSONController {

@RequestMapping(value = "{name}", method = RequestMethod.GET)
public @ResponseBody
Shop getShopInJSON(@PathVariable String name) {

Shop shop = new Shop();
shop.setName(name);
shop.setStaffName(new String[] { "hi", "hello" });

return shop;

}


}


Shop.java

package com.rajesh.common.model;

public class Shop {

String name;

String staffName[];
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getStaffName() {
return staffName;
}
public void setStaffName(String[] staffName) {
this.staffName = staffName;
}
public Shop() {



}

mvc-dispatcher-servlet.xml
<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.rajesh.common.controller" />

<mvc:annotation-driven />


</beans>

web.xml

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring Web MVC Application</display-name>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


</web-app>

You should be able to run this application on Apache Tomcat 7 from your Eclipse and should see the below output.




No comments:

Post a Comment