一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 如何使用Spring RestTemplate訪問(wèn)restful服務(wù)

如何使用Spring RestTemplate訪問(wèn)restful服務(wù)

2021-06-08 14:13低調(diào)的微胖 Java教程

這篇文章主要介紹了如何使用Spring RestTemplate訪問(wèn)restful服務(wù),詳細(xì)的介紹了什么是RestTemplate以及簡(jiǎn)單實(shí)現(xiàn),非常具有實(shí)用價(jià)值,需要的朋友可以參考下

一. 什么是resttemplate

spring's central class for synchronous client-side http access.
it simplifies communication with http servers, and enforces restful principles.
it handles http connections, leaving application code to provide urls(with possible template variables) and extract results.

 上面這段是resttemplate類(lèi)中的簡(jiǎn)單介紹,resttemplate是spring3.0后開(kāi)始提供的用于訪問(wèn) rest 服務(wù)的輕量級(jí)客戶端,相較于傳統(tǒng)的httpurlconnection、apache httpclient、okhttp等框架,resttemplate大大簡(jiǎn)化了發(fā)起http請(qǐng)求以及處理響應(yīng)的過(guò)程。本文關(guān)注resttemplate是如何使用的,暫不涉及內(nèi)部的實(shí)現(xiàn)原理。

二.一個(gè)簡(jiǎn)單的例子。

定義一個(gè)簡(jiǎn)單的restful接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@restcontroller
public class testcontroller
{
  @requestmapping(value = "testpost", method = requestmethod.post)
  public responsebean testpost(@requestbody requestbean requestbean)
  {
    responsebean responsebean = new responsebean();
    responsebean.setretcode("0000");
    responsebean.setretmsg("succ");
 
    return responsebean;
  }
}

使用resttemplate訪問(wèn)該服務(wù)

?
1
2
3
4
5
6
7
8
9
10
//請(qǐng)求地址
string url = "http://localhost:8080/testpost";
//入?yún)?/code>
requestbean requestbean = new requestbean();
requestbean.settest1("1");
requestbean.settest2("2");
requestbean.settest3("3");
 
resttemplate resttemplate = new resttemplate();
responsebean responsebean = resttemplate.postforobject(url, requestbean, responsebean.class);

從這個(gè)例子可以看出,使用resttemplate訪問(wèn)restful接口非常的簡(jiǎn)單粗暴無(wú)腦。(url, requestmap, responsebean.class)這三個(gè)參數(shù)分別代表 請(qǐng)求地址、請(qǐng)求參數(shù)、http響應(yīng)轉(zhuǎn)換被轉(zhuǎn)換成的對(duì)象類(lèi)型。

resttemplate方法的名稱(chēng)遵循命名約定,第一部分指出正在調(diào)用什么http方法,第二部分指示返回的內(nèi)容。本例中調(diào)用了resttemplate.postforobject方法,post指調(diào)用了http的post方法,object指將http響應(yīng)轉(zhuǎn)換為您選擇的對(duì)象類(lèi)型。還有其他很多類(lèi)似的方法,有興趣的同學(xué)可以參考官方api

三.手動(dòng)指定轉(zhuǎn)換器(httpmessageconverter)

我們知道,調(diào)用reseful接口傳遞的數(shù)據(jù)內(nèi)容是json格式的字符串,返回的響應(yīng)也是json格式的字符串。然而resttemplate.postforobject方法的請(qǐng)求參數(shù)requestbean和返回參數(shù)responsebean卻都是java類(lèi)。是resttemplate通過(guò)httpmessageconverter自動(dòng)幫我們做了轉(zhuǎn)換的操作。

默認(rèn)情況下resttemplate自動(dòng)幫我們注冊(cè)了一組httpmessageconverter用來(lái)處理一些不同的contenttype的請(qǐng)求。
如stringhttpmessageconverter來(lái)處理text/plain;mappingjackson2httpmessageconverter來(lái)處理application/json;mappingjackson2xmlhttpmessageconverter來(lái)處理application/xml。
你可以在org.springframework.http.converter包下找到所有spring幫我們實(shí)現(xiàn)好的轉(zhuǎn)換器。
如果現(xiàn)有的轉(zhuǎn)換器不能滿足你的需求,你還可以實(shí)現(xiàn)org.springframework.http.converter.httpmessageconverter接口自己寫(xiě)一個(gè)。詳情參考官方api

選好了httpmessageconverter后怎么把它注冊(cè)到我們的resttemplate中呢。

?
1
2
3
4
5
6
7
resttemplate resttemplate = new resttemplate();
//獲取resttemplate默認(rèn)配置好的所有轉(zhuǎn)換器
list<httpmessageconverter<?>> messageconverters = resttemplate.getmessageconverters();
//默認(rèn)的mappingjackson2httpmessageconverter在第7個(gè) 先把它移除掉
messageconverters.remove(6);
//添加上gson的轉(zhuǎn)換器
messageconverters.add(6, new gsonhttpmessageconverter());

這個(gè)簡(jiǎn)單的例子展示了如何使用gsonhttpmessageconverter替換掉默認(rèn)用來(lái)處理application/json的mappingjackson2httpmessageconverter。

四.設(shè)置底層連接方式

要?jiǎng)?chuàng)建一個(gè)resttemplate的實(shí)例,您可以像上述例子中簡(jiǎn)單地調(diào)用默認(rèn)的無(wú)參數(shù)構(gòu)造函數(shù)。這將使用java.net包中的標(biāo)準(zhǔn)java類(lèi)作為底層實(shí)現(xiàn)來(lái)創(chuàng)建http請(qǐng)求。

但很多時(shí)候我們需要像傳統(tǒng)的httpclient那樣設(shè)置http請(qǐng)求的一些屬性。resttemplate使用了一種很偷懶的方式實(shí)現(xiàn)了這個(gè)需求,那就是直接使用一個(gè)httpclient作為底層實(shí)現(xiàn)......

?
1
2
3
4
5
6
7
8
//生成一個(gè)設(shè)置了連接超時(shí)時(shí)間、請(qǐng)求超時(shí)時(shí)間、異常最大重試次數(shù)的httpclient
requestconfig config = requestconfig.custom().setconnectionrequesttimeout(10000).setconnecttimeout(10000).setsockettimeout(30000).build();
httpclientbuilder builder = httpclientbuilder.create().setdefaultrequestconfig(config).setretryhandler(new defaulthttprequestretryhandler(5, false));
httpclient httpclient = builder.build();
//使用httpclient創(chuàng)建一個(gè)clienthttprequestfactory的實(shí)現(xiàn)
clienthttprequestfactory requestfactory = new httpcomponentsclienthttprequestfactory(httpclient);
 //clienthttprequestfactory作為參數(shù)構(gòu)造一個(gè)使用作為底層的resttemplate
resttemplate resttemplate = new resttemplate(requestfactory);

五.設(shè)置攔截器(clienthttprequestinterceptor)

有時(shí)候我們需要對(duì)請(qǐng)求做一些通用的攔截設(shè)置,這就可以使用攔截器進(jìn)行處理。攔截器需要我們實(shí)現(xiàn)org.springframework.http.client.clienthttprequestinterceptor接口自己寫(xiě)。

舉個(gè)簡(jiǎn)單的例子,寫(xiě)一個(gè)在header中根據(jù)請(qǐng)求內(nèi)容和地址添加令牌的攔截器。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class tokeninterceptor implements clienthttprequestinterceptor
{
  @override
  public clienthttpresponse intercept(httprequest request, byte[] body, clienthttprequestexecution execution) throws ioexception
  {
    //請(qǐng)求地址
    string checktokenurl = request.geturi().getpath();
    //token有效時(shí)間
    int tttime = (int) (system.currenttimemillis() / 1000 + 1800);
    //請(qǐng)求方法名 post、get等
    string methodname = request.getmethod().name();
    //請(qǐng)求內(nèi)容
    string requestbody = new string(body);
    //生成令牌 此處調(diào)用一個(gè)自己寫(xiě)的方法,有興趣的朋友可以自行g(shù)oogle如何使用ak/sk生成token,此方法跟本教程無(wú)關(guān),就不貼出來(lái)了
    string token = tokenhelper.generatetoken(checktokenurl, tttime, methodname, requestbody);
    //將令牌放入請(qǐng)求header中
    request.getheaders().add("x-auth-token",token);
 
    return execution.execute(request, body);
  }
}

創(chuàng)建resttemplate實(shí)例的時(shí)候可以這樣向其中添加攔截器

?
1
2
3
resttemplate resttemplate = new resttemplate();
    //向resttemplate中添加自定義的攔截器
    resttemplate.getinterceptors().add(new tokeninterceptor());

六.總結(jié)

通過(guò)本章的講解,想必讀者初步的了解了如何使用resttemplate方便快捷的訪問(wèn)restful接口。其實(shí)resttemplate的功能非常強(qiáng)大,作者也僅僅學(xué)了點(diǎn)皮毛。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.jianshu.com/p/c9644755dd5e

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产高清在线精品一区 | 国产精品香蕉在线观看不卡 | 无人视频在线观看完整版高清 | 欧美坐爱 | 亚洲国产精品嫩草影院永久 | 美女毛片在线 | 女女同性做爰xxoo亲吻 | 欧美国产日本高清不卡 | 日本加勒比在线播放 | 欧美艳星kagneyiynn | 免费大片a一级一级 | 欧美精品久久久久久久影视 | 跪趴好紧h | 40分钟在线观看免费 | 亚洲国产精品久久卡一 | 美女的隐私视频免费看软件 | 欧美a级v片不卡在线观看 | 交换朋友夫妇3中文字幕 | 亚洲午夜久久久久国产 | 国内精品91东航翘臀女神在线 | 国内精品久久久久影院嫩草 | 国产精品不卡高清在线观看 | gay18高中生白袜xnxx动漫 | 国产成人综合亚洲亚洲欧美 | 精品无码人妻一区二区免费AV | 性鸥美 | 国产精品猎奇系列在线观看 | 五月天中文在线 | 日韩亚洲国产激情在线观看 | 农夫69小说小雨与农村老太 | 狠狠躁夜夜躁人人爽天天miya | 欧美在线播放成人免费 | 国产欧美综合精品一区二区 | 韩国最新三级网站在线播放 | 欧美a级在线 | 欧美胖逼 | 香蕉eeww99国产在线观看 | 秋霞在线观看成人高清视频51 | 国产中文在线视频 | 日本中文字幕不卡在线一区二区 | 精品湿 |