生活随笔
收集整理的這篇文章主要介紹了
RESTful Web Services in Spring 3(下)转载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上一篇我主要發了RESTful Web Services in Spring 3的服務端代碼,這里我準備寫客戶端的代碼。
?
上篇得連接地址為:http://yangjizhong.iteye.com/blog/600540
?
?
開始本篇了:
?
注:附件里有源碼,下載即可,依賴包請在spring網獲得,謝謝。
?
applicationContext.xml:
Xml代碼??
<? xml ?version ="1.0" ?encoding ="UTF-8" ?> ??< beans ?xmlns ="http://www.springframework.org/schema/beans" ??????xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" ?? ????xmlns:p ="http://www.springframework.org/schema/p" ?? ????xmlns:context ="http://www.springframework.org/schema/context" ?? ????xmlns:oxm ="http://www.springframework.org/schema/oxm" ?? ????xsi:schemaLocation ="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.5.xsd??? ????????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-2.5.xsd??? ????????????http://www.springframework.org/schema/oxm?http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"> ?? ?? ????< context:component-scan ?base-package ="com.informit.articleservice" ?/> ?? ?? ????< bean ?id ="restTemplate" ?class ="org.springframework.web.client.RestTemplate" > ?? ????????< property ?name ="messageConverters" > ?? ????????????< list > ?? ????????????????<!--?We?only?have?one?message?converter?for?the?RestTemplate,?namely?the?XStream?Marshller?-->?? ????????????????< bean ?class ="org.springframework.http.converter.xml.MarshallingHttpMessageConverter" > ?? ????????????????????< constructor-arg > ?? ????????????????????????< bean ?class ="org.springframework.oxm.xstream.XStreamMarshaller" > ?? ?????????????????????????????? ????????????????????????????<!--?Tell?XStream?to?find?the?alias?names?in?the?following?classes?-->?? ????????????????????????????< property ?name ="annotatedClasses" > ?? ????????????????????????????????< list > ?? ????????????????????????????????????< value > com.informit.articleservice.model.Article</ value > ?????????????????????????????? ????????????????????????????????????< value > com.informit.articleservice.model.Category</ value > ????????????????????????????? ????????????????????????????????</ list > ????????????????????????? ????????????????????????????</ property > ?? ????????????????????????</ bean > ?? ????????????????????</ constructor-arg > ?? ????????????????</ bean > ?? ????????????</ list > ?? ????????</ property > ?? ????</ bean > ?? ?? </ 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代碼??
package ?com.informit.articleservice.client;???? import ?java.util.HashMap;??import ?java.util.List;??import ?java.util.Map;???? import ?org.springframework.beans.factory.annotation.Autowired;??import ?org.springframework.stereotype.Component;??import ?org.springframework.web.client.RestTemplate;???? import ?com.informit.articleservice.model.Article;??import ?com.informit.articleservice.model.Category;???? @Component ("articleClient")??public ?class ?ArticleClient?{???? ????@Autowired ?? ????protected ?RestTemplate?restTemplate;?? ?? ????private ?final ?static ?String?articleServiceUrl?=?"http://localhost:8082/articleservice/";?? ?? ????@SuppressWarnings ("unchecked")?? ????public ?List<Category>?getCategories()?{?? ????????return ?restTemplate.getForObject(articleServiceUrl?+?"article",?List.class );?? ????}?? ?? ????public ?Article?getArticle(String?category,?int ?id)?{?? ????????return ?restTemplate.getForObject(articleServiceUrl?+?"article/{category}/{id}",?Article.class ,?category,?id);?? ????}?? ?? ????@SuppressWarnings ("unchecked")?? ????public ?void ?delCategories()?{?? ????????restTemplate.delete(articleServiceUrl?+?"article");?? ????}?? ?? ????@SuppressWarnings ("unchecked")?? ????public ?List<Category>?postCategories()?{?? ????????Map<String,?String>?params?=?new ?HashMap<String,?String>();?? ????????params.put("name",?"jizhong");?? ????????return ?restTemplate.postForObject(articleServiceUrl?+?"addarticle/{name}",?null ,?List.class ,?params);?? ?? ????}?? ?? }?? ?
?
在這里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代碼??
package ?com.informit.resttemplateexample;???? import ?java.util.List;???? import ?org.springframework.context.ApplicationContext;??import ?org.springframework.context.support.ClassPathXmlApplicationContext;???? import ?com.informit.articleservice.client.ArticleClient;??import ?com.informit.articleservice.model.Category;???? public ?class ?RestTemplateExample?{??????public ?static ?void ?main(String[]?args)?{?? ????????ApplicationContext?applicationContext?=?new ?ClassPathXmlApplicationContext("applicationContext.xml");?? ????????ArticleClient?articleClient?=?applicationContext.getBean("articleClient",?ArticleClient.class );?? ?? ????????//get?operate?? ????????//??????Article?article?=?articleClient.getArticle("fun",?1);?? ????????//??????System.out.println("Article:?"?+?article.getBody());?? ????????//?? ????????//??????List<Category>?categories?=?articleClient.getCategories();?? ????????//??????for?(Category?category?:?categories)?{?? ????????//??????????System.out.println("Category:?"?+?category);?? ????????//??????}?? ?? ????????//delete?operate?? ????????//articleClient.delCategories();?? ?? ????????//post?operate?? ????????List<Category>?categories?=?articleClient.postCategories();?? ?? ????}?? }?? ?
?
?
好了,然后本地跑一下就可以了,當然前提是一定把服務端跑起來哦....
?
注:詳細代碼在附件,JAR包還是自己下哈,終于寫完了,有點累,但是有收獲。
?
?
resttemplateexample_20100223.rar?(13.2 KB) 下載次數: 337
總結
以上是生活随笔 為你收集整理的RESTful Web Services in Spring 3(下)转载 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。