一. 什么是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