From a9d6ea06bc8e12fa9f9c78e8c7ce5538b83c6373 Mon Sep 17 00:00:00 2001 From: whyclxw <810412026@qq.com> Date: 星期二, 03 九月 2024 16:08:49 +0800 Subject: [PATCH] 批量操作 --- src/main/java/com/whyc/util/TestparamHttpUtil.java | 30 ++++++++++++++++++++++++++---- 1 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/whyc/util/TestparamHttpUtil.java b/src/main/java/com/whyc/util/TestparamHttpUtil.java index 27b3c53..af641df 100644 --- a/src/main/java/com/whyc/util/TestparamHttpUtil.java +++ b/src/main/java/com/whyc/util/TestparamHttpUtil.java @@ -4,9 +4,11 @@ import com.whyc.dto.ResultActmDto; import org.springframework.http.*; import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; public class TestparamHttpUtil { + //post璇锋眰浼犲叆form-data 鏍煎紡 @@ -16,8 +18,18 @@ // 璁剧疆浠ヨ〃鍗曠殑鏂瑰紡鎻愪氦 headers.add("Content-Type", MediaType.MULTIPART_FORM_DATA_VALUE); HttpEntity< MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(paramMap, headers); - ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity,String.class ); - String results= response.getBody(); + String results= null; + try { + ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity,String.class ); + results = response.getBody(); + } catch (RestClientException e) { + results=" {\n" + + " \"code\": \"210\",\n" + + " \"msg\": \"failed in setting test parameters\",\n" + + " \"data\": null,\n" + + " \"count\": 0\n" + + " }"; + } Object dto= ActionUtil.getGson().fromJson(results, ResultA200Dto.class); return dto; } @@ -29,8 +41,18 @@ // 璁剧疆浠ヨ〃鍗曠殑鏂瑰紡鎻愪氦 headers.add("Content-Type", MediaType.MULTIPART_FORM_DATA_VALUE); HttpEntity< MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(paramMap, headers); - ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity,String.class ); - String results= response.getBody(); + String results= null; + try { + ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity,String.class ); + results = response.getBody(); + } catch (RestClientException e) { + results=" {\n" + + " \"code\": \"210\",\n" + + " \"msg\": \"failed in setting test parameters\",\n" + + " \"data\": null,\n" + + " \"count\": 0\n" + + " }"; + } Object dto= ActionUtil.getGson().fromJson(results, ResultActmDto.class); return dto; } -- Gitblit v1.9.1