From 053d5042846ae96c9c7ba031bae9d9221745cd7f Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期五, 29 十一月 2024 15:20:58 +0800 Subject: [PATCH] 向通信中间件请求,有参数时用post,无参数时用Get. --- pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/rtuMw/Web2RtuMw.java | 38 ++++++++++++++++++++++++++++++-------- 1 files changed, 30 insertions(+), 8 deletions(-) diff --git a/pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/rtuMw/Web2RtuMw.java b/pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/rtuMw/Web2RtuMw.java index 86fc954..e4f1d05 100644 --- a/pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/rtuMw/Web2RtuMw.java +++ b/pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/rtuMw/Web2RtuMw.java @@ -57,20 +57,15 @@ * 鍚戦�氫俊涓棿浠跺彂閫乺tu杩滅▼鍗囩骇浠诲姟 * @param restTemplate SpringBoot鐨凴estTemplate * @param toMwUrl 鍒伴�氫俊涓棿浠剁殑web璇锋眰Url - * @param param 璇锋眰鍙傛暟 + * @param body 璇锋眰鏁版嵁 * @return */ - protected BaseResponse sendRequest2Mw(RestTemplate restTemplate, String toMwUrl, Object param) { + protected BaseResponse sendPostRequest2Mw(RestTemplate restTemplate, String toMwUrl, Object body) { String url = UriComponentsBuilder.fromUriString(toMwUrl) .build() .toUriString(); HttpHeaders headers = new HttpHeaders(); - HttpEntity<?> httpEntity ; - if(param != null){ - httpEntity = new HttpEntity<>(param, headers); - }else{ - httpEntity = new HttpEntity<>(headers); - } + HttpEntity<?> httpEntity = new HttpEntity<>(body, headers); ResponseEntity<BaseResponse> response = null; try { // 閫氳繃Post鏂瑰紡璋冪敤鎺ュ彛 @@ -87,6 +82,33 @@ } /** + * 鍚戦�氫俊涓棿浠跺彂閫乺tu杩滅▼鍗囩骇浠诲姟 + * @param restTemplate SpringBoot鐨凴estTemplate + * @param toMwUrl 鍒伴�氫俊涓棿浠剁殑web璇锋眰Url + * @return + */ + protected BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, String toMwUrl) { + String url = UriComponentsBuilder.fromUriString(toMwUrl) + .build() + .toUriString(); + HttpHeaders headers = new HttpHeaders(); + HttpEntity<?> httpEntity = new HttpEntity<>(headers); + ResponseEntity<BaseResponse> response = null; + try { + // 閫氳繃Post鏂瑰紡璋冪敤鎺ュ彛 + response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, BaseResponse.class); + } catch (Exception e) { + e.printStackTrace(); + return BaseResponseUtils.buildError("鍚庣绯荤粺鍑洪敊锛屼腑闂翠欢璋冪敤寮傚父"); + } + if(response == null){ + return BaseResponseUtils.buildError("鍚庣绯荤粺鍑洪敊锛屼腑闂翠欢璋冪敤寮傚父"); + }else{ + return response.getBody(); + } + } + + /** * 鍒涘缓澶栭儴鍛戒护锛堝彂缁欐帶鍒跺櫒锛� * @param code 鍛戒护code * @return -- Gitblit v1.8.0