java代码如何调用第三方服务呢?
下文笔者讲述java代码调用第三方服务的方法分享,如下所示
调用第三方服务的实现思路
使用HttpClient 和 RestTemplate
即可调用第三方服务
例:调用第三方服务的示例
private static ResultIf<PdfPlanIdsDto> get(String url) {
ResultIf<PdfPlanIdsDto> pdfPlanIdsDto = null;
try {
log.info("url={}", url);
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Content-Encoding", "UTF-8");
headers.add("Content-Type", "application/json; charset=UTF-8");
org.springframework.http.HttpEntity<String> requestEntity = new org.springframework.http.HttpEntity<>(null, headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,
requestEntity, String.class);
if (response.getStatusCode().equals(HttpStatus.OK)) {
String body = response.getBody();
//转对象
pdfPlanIdsDto= JSON.parseObject(body, new TypeReference<ResultIf<PdfPlanIdsDto>>() {
});
}
} catch (Exception e) {
log.error("{},请求服务器异常:{}", url,e);
}
return pdfPlanIdsDto;
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


