java 遠端呼叫第三方服務 阿新 • • 發佈:2021-10-19 1.httpclient 2.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;} 我是個雙魚座的小王子,沉浸在自己的程式碼世界裡,去探索這未知的世界,希望遇到更多的小夥伴一起前行!