• Spring 3 MVC And JSON Example


    In Spring 3, you can enable “mvc:annotation-driven” to support object conversion to/from JSON format, if Jackson JSON processor is existed on the project classpath.

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

    Technologies used :

    1. Spring 3.0.5.RELEASE
    2. Jackson 1.7.1
    3. JDK 1.6
    4. Eclipse 3.6
    5. Maven 3

    1. Project Dependencies

    To use JSON in Spring MVC, you need to include Jackson dependency.

        <properties>
            <spring.version>3.0.5.RELEASE</spring.version>
        </properties>
     
        <dependencies>
     
            <!-- Jackson JSON Mapper -->
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>1.7.1</version>
            </dependency>
     
            <!-- Spring 3 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>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
     

        </dependencies> 

    2. Model

    A simple POJO, later convert this object into JSON output.

    package com.mkyong.common.model;
     
    public class Shop {
     
        String name;
        String staffName[];
     
        //getter and setter methods
     

    3. Controller

    Add “@ResponseBody” in the return value, no much detail in the Spring documentation.

    As i know, when Spring see

    1. Jackson library existed on classpath
    2. “mvc:annotation-driven” is enabled
    3. Return method annotated with @ResponseBody

    It will handle the JSON conversion automatically.

    package com.mkyong.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.mkyong.common.model.Shop;
     
    @Controller
    @RequestMapping("/kfc/brands")
    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[]{"mkyong1", "mkyong2"});
     
            return shop;
     
        }
     

    4. mvc:annotation-driven

    Enable “mvc:annotation-driven” in your Spring configuration XML file.

    <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.mkyong.common.controller" />
     
        <mvc:annotation-driven />
     

    </beans> 

    5. Demo

    URL : http://localhost:8080/SpringMVC/rest/kfc/brands/kfc-kampar

    spring mvc and json demo

    Download Source Code

    Download it – SpringMVC-JSON-Example.zip (7 KB)
  • 相关阅读:
    ArcPad 10 的安装部署
    各种机械键盘轴的差别,究竟什么轴好
    default argument given of parameter 的问题
    Quartz中时间表达式的设置-----corn表达式
    图像切割之(一)概述
    SMTP协议分析
    Android学习小Demo(19)利用Loader来实时接收短信
    qml动画控制器AnimationController
    httpclient 文件上传
    Java习题10.24
  • 原文地址:https://www.cnblogs.com/chenying99/p/2783715.html
Copyright © 2020-2023  润新知