java代码如何调用第三方服务呢?

戚薇 Java经验 发布时间:2023-04-15 21:30:23 阅读数:17974 1
下文笔者讲述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;
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202304/16815654686249.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者