一款直擊痛點(diǎn)的優(yōu)秀 http 框架,讓我超高效完成了第三方接口的對(duì)接
背景
不同服務(wù)商API那么多的差異點(diǎn),如何才能維護(hù)一套不涉及業(yè)務(wù)的公共http調(diào)用套件。最好通過(guò)配置或者簡(jiǎn)單的參數(shù)就能區(qū)分開(kāi)來(lái)。進(jìn)行方便的調(diào)用?
于是,我發(fā)現(xiàn)了一款優(yōu)秀的開(kāi)源http框架,能屏蔽不同細(xì)節(jié)http api所帶來(lái)的所有差異。能通過(guò)簡(jiǎn)單的配置像調(diào)用rpc框架一樣的去完成極為復(fù)雜的http調(diào)用。
Forest

2.上手
??com.dtflys.forest
??spring-boot-starter-forest
??1.3.0
public?interface?MyClient?{
????@Request(url?=?"http://baidu.com")
????String?simpleRequest();
????@Request(
????????????url?=?"http://ditu.amap.com/service/regeo",
????????????dataType?=?"json"
????)
????Map?getLocation(@DataParam("longitude")?String?longitude,?@DataParam("latitude")?String?latitude);
??
}
@SpringBootApplication
@ForestScan(basePackages?=?"com.example.demo.forest")
public?class?DemoApplication?{
????public?static?void?main(String[]?args)?{
????????SpringApplication.run(DemoApplication.class,?args);
????}
}
這時(shí)候,你就可以從spring容器中注入你的代理接口,像調(diào)用本地方法一樣去調(diào)用http的api了
@Autowired
private?MyClient?myClient;
@Override
public?void?yourMethod?throws?Exception?{
????Map?result?=?myClient.getLocation("124.730329","31.463683");
????System.out.println(JSON.toJSONString(result,true));
}

3.特點(diǎn)
以Httpclient和OkHttp為后端框架
通過(guò)調(diào)用本地方法的方式去發(fā)送Http請(qǐng)求, 實(shí)現(xiàn)了業(yè)務(wù)邏輯與Http協(xié)議之間的解耦?
相比Feign更輕量,不依賴Spring Cloud和任何注冊(cè)中心?
支持所有請(qǐng)求方法:GET, HEAD, OPTIONS, TRACE, POST, DELETE, PUT, PATCH
支持靈活的模板表達(dá)式?
支持過(guò)濾器來(lái)過(guò)濾傳入的數(shù)據(jù)?
基于注解、配置化的方式定義Http請(qǐng)求?
支持Spring和Springboot集成?
實(shí)現(xiàn)JSON和XML的序列化和反序列化?
支持JSON轉(zhuǎn)換框架: Fastjson,Jackson, Gson?
支持JAXB形式的XML轉(zhuǎn)換?
支持SSL的單向和雙向加密?
支持http連接池的設(shè)定?
可以通過(guò)OnSuccess和OnError接口參數(shù)實(shí)現(xiàn)請(qǐng)求結(jié)果的回調(diào)?
配置簡(jiǎn)單,一般只需要@Request一個(gè)注解就能完成絕大多數(shù)請(qǐng)求的定義?
支持異步請(qǐng)求調(diào)用
4.兩個(gè)很棒的功能
4.1 模板表達(dá)式和參數(shù)的映射綁定功能
@Request(
????url?=?"${0}/send?un=${1}&pw=${2}&ph=${3}&ct=${4}",
????type?=?"get",
????dataType?=?"json"
)
public?Map?send(
????String?base,
????String?userName,
????String?password,
????String?phone,
????String?content
);
@Request(
????url?=?"${base}/send?un=${un}&pw=${pw}&ph=${3}&ct=${ct}",
????type?=?"get",
????dataType?=?"json"
)
public?Map?send(
????@DataVariable("base")?String?base,
????@DataVariable("un")?String?userName,
????@DataVariable("pw")?String?password,
????@DataVariable("ph")?String?phone,
????@DataVariable("ct")?String?content
);
@Request(
????url?=?"${base}/send",
????type?=?"get",
????dataType?=?"json"
)
public?Map?send(
????@DataVariable("base")?String?base,
????@DataParam("un")?String?userName,
????@DataParam("pw")?String?password,
????@DataParam("ph")?String?phone,
????@DataParam("ct")?String?content
);
@Request(
????url?=?"${base}/pay",
???contentType?=?"application/json",
????type?=?"post",
????dataType?=?"json",
????headers?=?{"Authorization:?${1}"},
????data?=?"${json($0)}"
)
public?PayResponse?pay(PayRequest?request,?String?auth);
4.2 對(duì)HTTPS的支持
@Request(
????url?=?"${base}/pay",
???contentType?=?"application/json",
????type?=?"post",
????dataType?=?"json",
???keyStore?=?"pay-keystore",
???data?=?"${json($0)}"
)
public?PayResponse?pay(PayRequest?request);
forest:
??...
??ssl-key-stores:
????-?id:?pay-keystore
??????file:?test.keystore
??????keystore-pass:?123456
??????cert-pass:?123456
??????protocols:?SSLv3
5.最后
逆鋒起筆是一個(gè)專注于程序員圈子的技術(shù)平臺(tái),你可以收獲最新技術(shù)動(dòng)態(tài)、最新內(nèi)測(cè)資格、BAT等大廠的經(jīng)驗(yàn)、精品學(xué)習(xí)資料、職業(yè)路線、副業(yè)思維,微信搜索逆鋒起筆關(guān)注!

評(píng)論
圖片
表情
