日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

RESTful Web Services in Spring 3(下)转载

發布時間:2025/7/14 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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(下)转载的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。