Loading
 
What's Next: View All Tutorial →

Spring MVC - JSON response with @ResponseBody annotation (Spring + JSON)

In this article you will learn how to get the Json response object in Spring MVC Refer Project for bean classes How to Create Business and DB Service Method Implementation using Spring MVC @Service annotation 1. Maven Dependency   ...

Published Date: 14/02/2015  Last Modified Date: 14/02/2015    New Post



In this article you will learn how to get the Json response object in Spring MVC

Refer Project for bean classes How to Create Business and DB Service Method Implementation using Spring MVC @Service annotation

1. Maven Dependency

 
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

jackson-mapper-asl - For json process
 

2. Dispatch-servlet.xml

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

       xmlns:util="http://www.springframework.org/schema/util"       
       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-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
		
    <mvc:annotation-driven  />
    <context:component-scan base-package="com.tutorial" />    
  
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
  
</beans>


3. Controller Class
 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tutorial.web;

import com.tutorial.domain.User;
import com.tutorial.service.UserService;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @Autowired
    public UserService userService;

    @RequestMapping("/getMapObject")
    public @ResponseBody
    Map getMapObject() {

        Map map = new HashMap();
        map.put("UserName", "Usha");
        map.put("Gender", "Female");
        return map;
    }

    @RequestMapping("/getUser")
    public @ResponseBody
    User getUser() {

        return userService.getUser();
    }

   
}

@ResponseBody - This parse the any given object and response it in JSON format. It support String response also


 

Output:


1) http://localhost:8084/SpringMVC/getHelloworld

Response:
Helloworld

-------------------
2) http://localhost:8084/SpringMVC/getMapObject

Response:
{"UserName":"Usha","Gender":"Female"}

-------------------
3) http://localhost:8084/SpringMVC/getUser

Reponse:
{"userName":"Usha","gender":"F","ageGroup":"24"}
 

Download Project:


Spring Json Response Project 
 
Awaiting for Administrator approval





Tags: Spring MVC

← Back to list


Related Post




×