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

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

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

    posts - 495,comments - 227,trackbacks - 0

    http://www.cnblogs.com/hoojo/archive/2011/06/10/2077422.html

    前面介紹過Spring的MVC結合不同的view顯示不同的數據,如:結合json的 view顯示json、結合xml的view顯示xml文檔。那么這些數據除了在WebBrowser中用JavaScript來調用以外,還可以用遠程 服務器的Java程序、C#程序來調用。也就是說現在的程序不僅在BS中能調用,在CS中同樣也能調用,不過你需要借助RestTemplate這個類來 完成。RestTemplate有點類似于一個WebService客戶端請求的模版,可以調用http請求的WebService,并將結果轉換成相應 的對象類型。至少你可以這樣理解!

     

    上一次博文介紹SpringMVC結合不同的View,顯示不同的數據。http://www.cnblogs.com/hoojo/archive/2011/04/29/2032571.html

     

    Email:hoojo_@126.com

    Blog:http://blog.csdn.net/IBM_hoojo

    http://hoojo.cnblogs.com/

     

    一、準備工作

    1、 下載jar包

    spring各版本jar下載地址:http://ebr.springsource.com/repository/app/library/detail?name=org.springframework.spring

    相關的依賴包也可以在這里找到:http://ebr.springsource.com/repository/app/library

     

    2、 需要jar包如下

    clip_image002

     

    3、 當前工程的web.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        
        <!-- 配置Spring核心控制器 -->
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/dispatcher.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
        
        <welcome-file-list>
          <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

    4、 WEB-INF中的dispatcher.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:util="http://www.springframework.org/schema/util"
        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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-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/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd">
     
        <context:component-scan base-package="com.hoo.*">
            <!-- 忽略這個類 -->
            <context:exclude-filter type="assignable" expression="com.hoo.client.RESTClient"/>
        </context:component-scan>
     
        <!-- annotation的方法映射適配器 -->
        <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
        
        <!-- xml視圖,XStreamMarshaller,可以轉換任何形式的java對象 -->
        <bean name="xStreamMarshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView">
            <property name="marshaller">
                <bean class="org.springframework.oxm.xstream.XStreamMarshaller">  
                    <!--  為了初始化XStreamMarshaller,這個類會把我們接口中得到結果以XML文檔形式展現出來 -->
                    <property name="autodetectAnnotations" value="true"/>  
                </bean>  
            </property>
        </bean>
            
        <!-- 視圖解析器,根據視圖的名稱new ModelAndView(name),在配置文件查找對應的bean配置 -->
        <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
            <property name="order" value="3"/>
        </bean>
     
        <!--  annotation默認的方法映射適配器 -->
        <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
            <property name="order" value="1" />
        </bean>
        
    </beans>

    5、 啟動后,可以看到index.jsp 沒有出現異常或錯誤。那么當前SpringMVC的配置就成功了。

     

    二、REST控制器實現

    REST控制器主要完成CRUD操作,也就是對于http中的post、get、put、delete。

    還有其他的操作,如head、options、trace。

    具體代碼:

    package com.hoo.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.servlet.ModelAndView;
     
    /**
     * <b>function:</b>SpringMVC REST示例
     * @author hoojo
     * @createDate 2011-6-9 上午11:34:08
     * @file RESTController.java
     * @package com.hoo.controller
     * @project SpringRestWS
     * @blog http://blog.csdn.net/IBM_hoojo
     * @email hoojo_@126.com
     * @version 1.0
     */
    @RequestMapping("/restful")
    @Controller
    public class RESTController {
        
        @RequestMapping(value = "/show", method = RequestMethod.GET)
        public ModelAndView show() {
            System.out.println("show");
            ModelAndView model = new ModelAndView("xStreamMarshallingView");
            model.addObject("show method");
            return model; 
        }
        
        @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
        public ModelAndView getUserById(@PathVariable String id) {
            System.out.println("getUserById-" + id);
            ModelAndView model = new ModelAndView("xStreamMarshallingView");
            model.addObject("getUserById method -" + id);
            return model; 
        }
        
        @RequestMapping(value = "/add", method = RequestMethod.POST)
        public ModelAndView addUser(String user) {
            System.out.println("addUser-" + user);
            ModelAndView model = new ModelAndView("xStreamMarshallingView");
            model.addObject("addUser method -" + user);
            return model; 
        }
        
        @RequestMapping(value = "/edit", method = RequestMethod.PUT)
        public ModelAndView editUser(String user) {
            System.out.println("editUser-" + user);
            ModelAndView model = new ModelAndView("xStreamMarshallingView");
            model.addObject("editUser method -" + user);
            return model;
        }
        
        @RequestMapping(value = "/remove/{id}", method = RequestMethod.DELETE)
        public ModelAndView removeUser(@PathVariable String id) {
            System.out.println("removeUser-" + id);
            ModelAndView model = new ModelAndView("xStreamMarshallingView");
            model.addObject("removeUser method -" + id);
            return model;
        }
    }

    上面的方法對應的http操作:

    /show -> get 查詢
    /get/id -> get 查詢
    /add -> post 添加
    /edit -> put 修改
    /remove/id -> delete 刪除

    在這個方法中,就可以看到RESTful風格的url資源標識

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public ModelAndView getUserById(@PathVariable String id) {
        System.out.println("getUserById-" + id);
        ModelAndView model = new ModelAndView("xStreamMarshallingView");
        model.addObject("getUserById method -" + id);
        return model; 
    }

    value=”/get/{id}”就是url中包含get,并且帶有id參數的get請求,就會執行這個方法。這個url在請求的時候,會通過 Annotation的@PathVariable來將url中的id值設置到getUserById的參數中去。 ModelAndView返回的視圖是xStreamMarshallingView是一個xml視圖,執行當前請求后,會顯示一篇xml文檔。文檔的內 容是添加到model中的值。

     

    三、利用RestTemplate調用REST資源

    代碼如下:

    package com.hoo.client;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.web.client.RestTemplate;
     
    /**
     * <b>function:</b>RestTemplate調用REST資源
     * @author hoojo
     * @createDate 2011-6-9 上午11:56:16
     * @file RESTClient.java
     * @package com.hoo.client
     * @project SpringRestWS
     * @blog http://blog.csdn.net/IBM_hoojo
     * @email hoojo_@126.com
     * @version 1.0
     */
    @Component
    public class RESTClient {
        
        @Autowired
        private RestTemplate template;
        
        private final static String url = "http://localhost:8080/SpringRestWS/restful/";
        
        public String show() {
            return template.getForObject(url + "show.do", String.class, new String[]{});
        }
        
        public String getUserById(String id) {
            return template.getForObject(url + "get/{id}.do", String.class, id); 
        }
        
        public String addUser(String user) {
            return template.postForObject(url + "add.do?user={user}", null, String.class, user);
        }
        
        public String editUser(String user) {
            template.put(url + "edit.do?user={user}", null, user);
            return user;
        }
        
        public String removeUser(String id) {
            template.delete(url + "/remove/{id}.do", id);
            return id;
        }
    }

    RestTemplate的getForObject完成get請求、postForObject完成post請求、put對應的完成put請求、 delete完成delete請求;還有execute可以執行任何請求的方法,需要你設置RequestMethod來指定當前請求類型。

    RestTemplate.getForObject(String url, Class<String> responseType, String... urlVariables)

    參數url是http請求的地址,參數Class是請求響應返回后的數據的類型,最后一個參數是請求中需要設置的參數。

    template.getForObject(url + "get/{id}.do", String.class, id);

    如上面的參數是{id},返回的是一個string類型,設置的參數是id。最后執行該方法會返回一個String類型的結果。

     

    下面建立一個測試類,完成對RESTClient的測試。代碼如下:

    package com.hoo.client;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;
     
    /**
     * <b>function:</b>RESTClient TEST
     * @author hoojo
     * @createDate 2011-6-9 下午03:50:21
     * @file RESTClientTest.java
     * @package com.hoo.client
     * @project SpringRestWS
     * @blog http://blog.csdn.net/IBM_hoojo
     * @email hoojo_@126.com
     * @version 1.0
     */
    @ContextConfiguration("classpath:applicationContext-*.xml")
    public class RESTClientTest extends AbstractJUnit38SpringContextTests {
        
        @Autowired
        private RESTClient client;
        
        public void testShow() {
            System.out.println(client.show());
        }
        
        public void testGetUserById() {
            System.out.println(client.getUserById("abc"));
        }
        
        public void testAddUser() {
            System.out.println(client.addUser("jack"));
        }
        
        public void testEditUser() {
            System.out.println(client.editUser("tom"));
        }
        
        public void testRemoveUser() {
            System.out.println(client.removeUser("aabb"));
        }
    }

    我們需要在src目錄下添加applicationContext-beans.xml完成對restTemplate的配置。restTemplate需要配置MessageConvert將返回的xml文檔進行轉換,解析成JavaObject。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        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">
        
        <context:component-scan base-package="com.hoo.*"/>
        
        <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
            <property name="messageConverters">
                <list>
                    <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                        <property name="marshaller" ref="xStreamMarshaller"/>
                        <property name="unmarshaller" ref="xStreamMarshaller"/>
                    </bean>
                </list>
            </property>
        </bean>
        
        <bean id="xStreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
            <property name="annotatedClasses">
                <array>                
                </array>
            </property>
        </bean>
    </beans>

    上面配置了xStreamMarshaller是和RESTController中的ModelAndView的view對應的。因為那邊是用 xStreamMarshaller進行編組的,所以RestTemplate這邊也需要用它來解組。RestTemplate還指出其他的 MarshallingHttpMessageConverter;

    posted on 2012-09-05 13:39 SIMONE 閱讀(2593) 評論(0)  編輯  收藏 所屬分類: JAVA
    主站蜘蛛池模板: 亚洲人成无码网WWW| 黄页免费视频播放在线播放| 亚洲无码视频在线| 男人的好免费观看在线视频| 国内精品一级毛片免费看| 亚洲av无码有乱码在线观看| 亚洲精品动漫在线| 国产亚洲精久久久久久无码| 亚洲人成电影在线播放| 老司机永久免费网站在线观看| 国产成人精品免费视频大| 免费日本一区二区| 国产精品极品美女自在线观看免费| 亚洲精品精华液一区二区| 亚洲人成综合在线播放| 亚洲精品福利网泷泽萝拉| 国产AV无码专区亚洲AVJULIA| 国产亚洲精午夜久久久久久| 免费在线观看黄色毛片| 午夜一级免费视频| 久久久久久国产精品免费免费| 51视频精品全部免费最新| 日本一卡精品视频免费| 四虎成人精品永久免费AV| 野花香在线视频免费观看大全 | 国产精品免费观看调教网| 免费无码又爽又刺激网站直播| 国产精品内射视频免费| jizz在线免费观看| 久久免费视频一区| 大地资源在线资源免费观看| 永久免费av无码入口国语片| 黄页免费在线观看 | 好看的电影网站亚洲一区| 亚洲色欲色欲www在线丝| 亚洲色欲久久久综合网| 亚洲精品制服丝袜四区| 亚洲国产美国国产综合一区二区| 久久精品亚洲视频| 亚洲精品成人图区| 国产精品亚洲综合久久 |