RestTemplate實(shí)現(xiàn)訪問遠(yuǎn)程Http服務(wù)

RestTemplate是spring提供的用于訪問Rest服務(wù)的客戶端,RestTemplate提供了多種便捷訪問遠(yuǎn)程Http服務(wù)的方法,能夠大大提高客戶端的編寫效率。
調(diào)用RestTemplate的默認(rèn)構(gòu)造函數(shù),RestTemplate對象在底層通過使用Java.net包下的實(shí)現(xiàn)創(chuàng)建HTTP 請求,可以通過使用ClientHttpRequestFactory指定不同的HTTP請求方式。ClientHttpRequestFactory接口主要提供了兩種實(shí)現(xiàn)方式:
一種是SimpleClientHttpRequestFactory,使用J2SE提供的方式(既java.net包提供的方式)創(chuàng)建底層的Http請求連接。
一種方式是使用HttpComponentsClientHttpRequestFactory方式,底層使用HttpClient訪問遠(yuǎn)程的Http服務(wù),使用HttpClient可以配置連接池和證書等信息。[本段抄]

本文使用SimpleClientHttpRequestFactory實(shí)現(xiàn)ClientHttpRequestFactory接口

public class SimpleRestClient {

    private static RestTemplate restTemplate;

    private SimpleRestClient() {

    }

    static {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(15000);
        factory.setConnectTimeout(15000);
        restTemplate = new RestTemplate(factory);

        CustomHttpMessageConverter converter = new CustomHttpMessageConverter();
        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(converter);
        restTemplate.setMessageConverters(converters);

    }

    public static RestTemplate getClient() {
        return restTemplate;
    }

}

get方式

    //Object 對象為Response返回的具體類型,需根據(jù)實(shí)際返回對象創(chuàng)建實(shí)體類等
    //參數(shù)直接放在URL中
    Object result = restTemplate.getForObject("http://localhost:8080/xxx/test?appid=1", Object.class);
    
    // 參數(shù)使用可變參數(shù) ...
    int appid = 1;
    Object result = restTemplate.getForObject("http://localhost:8080/xxx/test?appid={appid}", Object.class, appid);

    //參數(shù)使用MAP傳遞
    Map<String,Object> urlVariables = new HashMap<String,Object>();
    urlVariables.put("appid", 1);
    Object result = restTemplate.getForObject("http://localhost:8080/xxx/test", Object.class, urlVariables);

post方式

    //postForObject 使用方式與get基本相同
    Object result = restTemplate
            .postForObject("http://localhost:8080/xxx/test", null, UmBenefitRuleResponse.class);

put方式

    //參數(shù)同上三種方式
    restTemplate.put("http://localhost:8080/xxx/test" ,null);

delete方式

    //參數(shù)同上三種方式
    restTemplate.delete("http://localhost:8080/xxx/test?appid={appid}",appid);

推薦博文:RestTemplate實(shí)踐

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,536評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,922評論 25 709
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,262評論 6 342
  • 大好時(shí)光過成了將死之生活 感覺現(xiàn)在最可悲的就是沒有方向、沒有目標(biāo)、也沒有喜歡
    小太陽蒙蒙噠閱讀 165評論 0 1
  • 開貼記錄我的寫作歷程,每天更新! 1月目標(biāo):碼字10萬,粉絲增加100個(gè),不找借口,鍛煉執(zhí)行力。 第1天:碼字17...
    孽因閱讀 230評論 51 0

友情鏈接更多精彩內(nèi)容