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 閱讀(2595) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 精河县| 宝山区| 尚义县| 周至县| 吉安市| 得荣县| 闽清县| 叶城县| 嘉善县| 象山县| 巴南区| 临泽县| 久治县| 安康市| 师宗县| 祥云县| 隆昌县| 东兴市| 都江堰市| 班戈县| 嵩明县| 临清市| 原阳县| 大连市| 田东县| 潞城市| 华池县| 涟水县| 民丰县| 盘山县| 汶川县| 武安市| 华安县| 织金县| 兴海县| 嘉义县| 昌江| 读书| 灵石县| 平定县| 赞皇县|