RestTemplate 调用外部方法

 1  /**
 2      * 定义调用外部接口实例
 3      */
 4     private static final RestTemplate INSTANCE = new RestTemplate();
 5 
 6     public static RestTemplate getInstance() {
 7         return HttpRequestService.INSTANCE;
 8     }
 9 
10     /**
11      * @param uri          uri
12      * @param param        请求参数
13      * @param responseType 返回值类型
14      * @param <T>
15      * @return
16      */
17     public <T> T request(String uri, String param, Class<T> responseType) {
18         // 设置请求头信息
19         HttpHeaders headers = new HttpHeaders();
20         MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
21         headers.setContentType(type);
22         headers.add("Accept-Charset", MediaType.APPLICATION_JSON.toString());
23         // 封装参数
24         HttpEntity<String> object = new HttpEntity<>(param, headers);
25         String url = YTX_URL + uri;
26         LOGGER.info("request url:{},appkey:{},ts:{},msgdgt:{}", url, appKey, ts, msgdgt);
27         ResponseEntity<T> exchange = getInstance().exchange(url, HttpMethod.DELETE, object, responseType);
28         LOGGER.info("responseResult code:{},body:{}", exchange.getStatusCode(), exchange.getBody());
29         return exchange.getBody();
30     }

 

posted @ 2020-10-14 17:19  明天,你好啊  阅读(604)  评论(0编辑  收藏  举报