This example of Spring4 MVC + Maven + XML based configuration. So many time we required return response in JSON format, Spring provide easy way to convert your response in JSON format. In this example we are using jackson library to convert model in JSON.

Technology Used:

  1. String 4.3.0
  2. Maven 2.3.2
  3. JSON (jsckson library)
  4. Tomcat 7
  5. Java 8
  6. IntelljIDEA 14

Project Structure:

spring-json-example

spring-json-example

Dependency:

pom.xml

Bellow library must be require in your class path:

<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.springapp</groupId>
    <artifactId>spring-json-example</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>spring-json-example</name>

    <properties>
        <spring.version>4.3.0.RELEASE</spring.version>
        <jaction.xml.version>2.7.3</jaction.xml.version>
        <maven-compiler-plugin-version>2.3.2</maven-compiler-plugin-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jaction.xml.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jaction.xml.version}</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>spring-hello-work</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin-version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Java Code:

EmployeeController.java

@ResponseBody annotation is used to convert Employee POJO into JSON. Spring automatically identify type of object and  convert Employee Array to JSON array.

package com.springdemo.controller;

import com.springdemo.model.Employee;
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;

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

@Controller
@RequestMapping("employee")
public class EmployeeController {

 @RequestMapping(value = "list",method = RequestMethod.GET)
 @ResponseBody
 public List<Employee> list() {

 List<Employee> employeeList=new ArrayList<>();

 Employee employee1=new Employee();
 employee1.setEmployeeId(1);
 employee1.setEmployeeName("Subhash");
 employee1.setEmployeeSalary(1000);

 Employee employee2=new Employee();
 employee2.setEmployeeId(2);
 employee2.setEmployeeName("Nitin");
 employee2.setEmployeeSalary(2000);

 Employee employee3=new Employee();
 employee3.setEmployeeId(2);
 employee3.setEmployeeName("Mahesh");
 employee3.setEmployeeSalary(2000);

 employeeList.add(employee1);
 employeeList.add(employee2);
 return employeeList;

 }
}

Employee.java

package com.springdemo.model;

/**
 * Created by Lenovo on 19-01-2017.
 */
public class Employee {
    private int employeeId;
    private String employeeName;
    private long employeeSalary;

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public long getEmployeeSalary() {
        return employeeSalary;
    }

    public void setEmployeeSalary(long employeeSalary) {
        this.employeeSalary = employeeSalary;
    }
}

Configurations:

web.xml

<web-app 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 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>/</url-pattern>
  </servlet-mapping>
</web-app>

spring-dispatcher-servlet.xml

<mvc:annotation-driven/>must require to enable @ResponseBody annotations. When we enable <mvc:annotation-driven/> at that time spring will register jackson transformer so we response will be converted in JSON format using @ResponseBody annotation at controller level.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.springdemo"/>

    <mvc:annotation-driven/> <!-- Must require to enable @ResponseBody annotation -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

Output:

spring-json-example-output

spring-json-example-output

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *