<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-193  評論-715  文章-1  trackbacks-0
    老外寫的文章真的很易懂!  
    原文地址:http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/


    Published: July 28, 2011 , Updated: July 27, 2011 , Author: mkyong

     

    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 :

    Spring 3.0.5.RELEASE
    Jackson 1.7.1
    JDK 1.6
    Eclipse 3.6
    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

    Jackson library existed on classpath
    “mvc:annotation-driven” is enabled
    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


    Download Source Code

    Download it – SpringMVC-JSON-Example.zip (7 KB)
    References

    mvc-annotation-driven documentation
    High-performance JSON processor
    Spring MVC and XML example
    posted on 2012-06-04 23:06 Robin's Programming World 閱讀(6767) 評論(1)  編輯  收藏 所屬分類: Java

    評論:
    # re: Spring 3 MVC and JSON example 2012-06-07 12:46 | rox
    <!-- 根據客戶端的不同的請求決定不同的view進行響應, 如 /blog/1.json /blog/1.xml -->
    <bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="ignoreAcceptHeader" value="true" />
    <property name="defaultContentType" value="text/html" />

    <!-- 擴展名至mimeType的映射,即 /user.json => application/json -->
    <property name="mediaTypes">
    <map>
    <entry key="json" value="application/json" />
    <entry key="xml" value="application/xml" />
    </map>
    </property>
    <!-- 用于開啟 /userinfo/123?format=json 的支持 -->
    <property name="favorParameter" value="true" />
    <!-- -->
    <property name="viewResolvers">
    <list>
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
    value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp"></property>
    </bean>
    </list>
    </property>
    <property name="defaultViews">
    <list>
    <!-- for application/json -->
    <bean
    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    <!-- for application/xml -->
    <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
    <property name="marshaller">
    <bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
    </property>
    </bean>
    </list>
    </property>
    </bean>


    更新設置重啟后,然后訪問
    http://localhost:8080/SpringMVC/rest/kfc/brands/kfc-kampar.json
    http://localhost:8080/SpringMVC/rest/kfc/brands/kfc-kampar.xml
    試試  回復  更多評論
      
    主站蜘蛛池模板: 亚洲自偷自偷偷色无码中文| 免费萌白酱国产一区二区三区 | 亚洲国产中文在线二区三区免| 亚洲国产成人精品女人久久久| 4399好看日本在线电影免费| 巨胸喷奶水视频www免费视频| 亚洲AV无码一区二区三区性色 | 在线播放国产不卡免费视频| 亚洲国产视频久久| 亚洲网红精品大秀在线观看| 亚洲中文字幕无码一区二区三区| 国产免费爽爽视频免费可以看| 成人免费AA片在线观看| 8090在线观看免费观看| 在线免费观看h片| 国产免费一区二区三区免费视频| 老司机午夜在线视频免费| 亚洲国产一区二区三区在线观看| 亚洲一区二区三区播放在线| 亚洲美女免费视频| 亚洲经典在线观看| 亚洲成人黄色在线观看| 91大神亚洲影视在线| 亚洲资源在线观看| 久久久久亚洲精品日久生情| 亚洲AV无码一区二区三区DV| 亚洲精品V欧洲精品V日韩精品| 久久精品国产亚洲精品| 久久亚洲国产精品五月天婷| 亚洲精品视频久久久| 中文字幕第13亚洲另类| 怡红院亚洲怡红院首页| 丝袜熟女国偷自产中文字幕亚洲| 亚洲人成网站在线观看青青| 国产精品亚洲mnbav网站| 日韩一卡2卡3卡4卡新区亚洲| 中文字幕亚洲天堂| 国产亚洲婷婷香蕉久久精品| 亚洲国产精品无码av| 久久久久久亚洲精品成人| 亚洲特级aaaaaa毛片|