Annotation HttpClienthtml
本內容不保證正確性,若有問題請及時提出apache
通過前面四篇博客的鋪墊,如今給出帶有標記的HttpClient的實現。json
1. 帶標記的HttpClient的需求和定義api
1) Http的訪問方法服務器
Http的訪問方法主要有兩種,分別是Get和Post,對應的定義了兩個Annotation,@Get和@Post分別表示實際調用執行的Http消息類型。網絡
2) Http的URLmybatis
URL用於定位Http訪問的網絡服務器地址和方法,對應定義了@URL。併發
3) Http參數的類型app
在Http訪問中,參數能夠放在URL中(@ParamUrl)、放在QueryString中(@ParamQuery)、放在Head中(@ParamHead)、放在Body中(@ParamBody)。學習
此外對於Multipart類型(文件類型)的消息,定義了@ParamFile和HttpFile。
4) 默認的Http參數
不少狀況下,在訪問遠程的Http請求可使用默認的參數,針對這種狀況,定義了@ParamDefault和@ParamDefaults。
5) 返回類型
咱們只實現了兩種返回類型,一種是String,一種是byte[]。若是須要其餘複雜類型例如xml、json等,能夠先返回String,而後再轉換成須要的類型。若是須要返回的是文件類型,那麼返回類型爲byte[].
2. 項目中的類說明
MapperProxy和MapperMethod,用於建立相應接口的代理,在代理中分析接口的Annotation,組織內部參數,生成調用Http的相應參數。
RestClient和HttpTemplate提供了MapperMethod訪問Http服務須要的接口,而DefaultRestClient和DefaultHttpTemplate分別是上述兩接口的實現。其中DefaultRestClient使用apache HttpClient的鏈接池形式實現的,而DefaultHttpTemplate則是經過繼承Spring的HttpTemplate實現的,能夠根據須要替換掉這兩個實現(好比使用併發的HttpClient)。
3. 項目使用舉例
3.1 簡單的weather訪問實例
Dao定義以下:
@URL("http://m.weather.com.cn/data/{city}.html")
publicinterfaceWeatherDao {
@Get
String getWeather(@ParamUrl("city")int cityCode);
}
測試程序以下執行:
WeatherDao weather = MapperProxy.newMapperProxy(WeatherDao.class,new DefaultHttpTemplate());
System.out.println(weather.getWeather(101010100));
3.2 簡單的微博訪問實例
Dao定義:
@URL("https://api.weibo.com/2")
publicinterface WeiBoHttpDao {
//獲取短url,使用https
@Get("/short_url/shorten.json")
public String getshortUrl(@ParamQuery("access_token") String accessToken,
@ParamQuery("source")long source,
@ParamQuery("url_long") String urlLong);
}
測試程序以下執行:
WeiBoHttpDao dao = MapperProxy.newMapperProxy(WeiBoHttpDao.class,new DefaultHttpTemplate());
System.out.println(dao.getshortUrl("token",xxxx,"url"));
3.3 人人網的訪問
@URL("http://api.m.renren.com/api")
@ParamDefaults({
@ParamDefault(paramname="v",value="1.0",type=ParamType.BODY),
@ParamDefault(paramname="format",type=ParamType.BODY,value="JSON")})
publicinterface RenRenHttpDao {
@Post("/photos/uploadbin")
@ParamDefaults({
@ParamDefault(paramname="voice_rate",value="44100",type=ParamType.BODY),
@ParamDefault(paramname="default_album_switch",value="2",type=ParamType.BODY),
@ParamDefault(paramname="from",value="xxxxx",type=ParamType.BODY)
})
public String share(//分享
@ParamBody("access_token") String accessToken,
@ParamBody("call_id") String callId,
@ParamBody("caption") String caption,
@ParamFile("data") HttpFile data,
@ParamFile("voicedata") HttpFile voiceData,
@ParamBody("voice_length")long voiceLength,
@ParamBody("voice_size")long voiceSize,
@ParamBody("sig") String sig
);
@Post("/user/getInfo")
public String userInfo(//獲取用戶信息
@ParamBody("access_token") String accessToken,
@ParamBody("call_id") String callId,
@ParamBody("type")long type,
@ParamBody("sig") String sig);
@Post("/friends/getFriends")
@ParamDefaults({
@ParamDefault(paramname="hasGender",value="1",type=ParamType.BODY),
@ParamDefault(paramname="pageSize",value="600",type=ParamType.BODY)
})
public String getFriends(//獲取好友列表
@ParamBody("access_token") String accessToken,
@ParamBody("call_id")long callId,
@ParamBody("page")long page,
@ParamBody("sig") String sig
);
}
4. 小結
經過五篇博客,我但願可以將如何編寫annotation的應用說明清楚,若是你理解這個過程,會以爲很簡單,可是要徹底說明白倒是一件極其複雜的事情,經過五篇博客的形式,展示了我腦中的annotation應用的實現方式,但願對你們有幫助。
文中敘述的這種方式,源自於mybatis的源碼,你們也能夠去參考。
最後,我將例子中的Annotation HttpClient的源碼發出來供你們參考。源碼不多,也很簡單,而且在我以爲必要的地方加了註釋,但願能幫助你們一塊兒探討學習。
源碼在個人csdn下載裏面。http://download.csdn.net/detail/guanxinquan/6019591