RESTful Web Services in Spring 3(下)

上一篇我主要发了RESTful Web Services in Spring 3的服务端代码,这里我准备写客户端的代码。

 

上篇得连接地址为:http://yangjizhong.iteye.com/blog/600540

 

 

开始本篇了:

 

注:附件里有源码,下载即可,依赖包请在spring网获得,谢谢。

 

applicationContext.xml:

Xml代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:oxm="http://www.springframework.org/schema/oxm"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  9.             http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">  
  10.   
  11.     <context:component-scan base-package="com.informit.articleservice" />  
  12.   
  13.     <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">  
  14.         <property name="messageConverters">  
  15.             <list>  
  16.                 <!-- We only have one message converter for the RestTemplate, namely the XStream Marshller -->  
  17.                 <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">  
  18.                     <constructor-arg>  
  19.                         <bean class="org.springframework.oxm.xstream.XStreamMarshaller">  
  20.                               
  21.                             <!-- Tell XStream to find the alias names in the following classes -->  
  22.                             <property name="annotatedClasses">  
  23.                                 <list>  
  24.                                     <value>com.informit.articleservice.model.Article</value>                              
  25.                                     <value>com.informit.articleservice.model.Category</value>                             
  26.                                 </list>                         
  27.                             </property>  
  28.                         </bean>  
  29.                     </constructor-arg>  
  30.                 </bean>  
  31.             </list>  
  32.         </property>  
  33.     </bean>  
  34.   
  35. </beans>  

 

 

applicationContext.xml声明了一个bean,名restTemplate,implemented by org.springframework.web.client.RestTemplate,RestTemplate 类提供了一个setter来声明message converters,在这个属性我们提供一个包含一个简单bean的list:a MarshallingHttpMessageConverter,这是你的实体信息声明的地方

 

restTemplate bean声明后,ArticleClient 使用了restTemplate来调取ArticleService:

 

Java代码    收藏代码
  1. package com.informit.articleservice.client;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.springframework.beans.factory.annotation.Autowired;  
  8. import org.springframework.stereotype.Component;  
  9. import org.springframework.web.client.RestTemplate;  
  10.   
  11. import com.informit.articleservice.model.Article;  
  12. import com.informit.articleservice.model.Category;  
  13.   
  14. @Component("articleClient")  
  15. public class ArticleClient {  
  16.   
  17.     @Autowired  
  18.     protected RestTemplate restTemplate;  
  19.   
  20.     private final static String articleServiceUrl = "http://localhost:8082/articleservice/";  
  21.   
  22.     @SuppressWarnings("unchecked")  
  23.     public List<Category> getCategories() {  
  24.         return restTemplate.getForObject(articleServiceUrl + "article", List.class);  
  25.     }  
  26.   
  27.     public Article getArticle(String category, int id) {  
  28.         return restTemplate.getForObject(articleServiceUrl + "article/{category}/{id}", Article.class, category, id);  
  29.     }  
  30.   
  31.     @SuppressWarnings("unchecked")  
  32.     public void delCategories() {  
  33.         restTemplate.delete(articleServiceUrl + "article");  
  34.     }  
  35.   
  36.     @SuppressWarnings("unchecked")  
  37.     public List<Category> postCategories() {  
  38.         Map<String, String> params = new HashMap<String, String>();  
  39.         params.put("name", "jizhong");  
  40.         return restTemplate.postForObject(articleServiceUrl + "addarticle/{name}", null, List.class, params);  
  41.   
  42.     }  
  43.   
  44. }  

 

 

在这里RestTemplate是自动加载的(auto-wired),你会注意到ArticleClient被加上了@Component annotation而且applicationContext.xml自动扫描com.informit.articleservice包或他的子包,因此当ArticleClient通过application context被loaded时,他会自动作为一个接口来实现RestTemplate实例

 

RestTemplate的相关使用的方法在文档中是这样写的:

 

delete(): deletes an object hosted by the web service 
getForObject(): executes the HTTP GET command and returns the requested object 
headForHeaders(): executes the HTTP HEAD command and returns the headers for the requested service 
optionsForAllow(): executes the HTTP OPTIONS command and returns list of content types the the request service allows 
postForLocation: executes the HTTP POST command and returns the location header value 
postForObject(): executes the HTTP POST command and returns the object at the specified URL 
put(): executes the HTTP PUT command and sends the specified object to the web service 
execute(): provides fine grained control if one of the aforementioned methods does not suit your needs

 

接下来列出测试类:

 

Java代码    收藏代码
  1. package com.informit.resttemplateexample;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. import com.informit.articleservice.client.ArticleClient;  
  9. import com.informit.articleservice.model.Category;  
  10.   
  11. public class RestTemplateExample {  
  12.     public static void main(String[] args) {  
  13.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");  
  14.         ArticleClient articleClient = applicationContext.getBean("articleClient", ArticleClient.class);  
  15.   
  16.         //get operate  
  17.         //      Article article = articleClient.getArticle("fun", 1);  
  18.         //      System.out.println("Article: " + article.getBody());  
  19.         //  
  20.         //      List<Category> categories = articleClient.getCategories();  
  21.         //      for (Category category : categories) {  
  22.         //          System.out.println("Category: " + category);  
  23.         //      }  
  24.   
  25.         //delete operate  
  26.         //articleClient.delCategories();  
  27.   
  28.         //post operate  
  29.         List<Category> categories = articleClient.postCategories();  
  30.   
  31.     }  
  32. }  

 

 

 

好了,然后本地跑一下就可以了,当然前提是一定把服务端跑起来哦....

 

注:详细代码在附件,JAR包还是自己下哈,终于写完了,有点累,但是有收获。

 

 

你可能感兴趣的:(web services)