From 45100cee3d67dab03f7b806e476709fee00b03c6 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Mon, 27 May 2024 11:48:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92=E6=8A=A5=E5=91=8A=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1037585 --user=陈建星 【测试跟踪】github#29596,测试计划报告导出报错 https://www.tapd.cn/55049933/s/1521192 --- .../java/io/metersphere/utils/JsonUtils.java | 2 +- .../autoconfigure/RestTemplateConfig.java | 29 +++++++++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/JsonUtils.java b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/JsonUtils.java index e61fbd97e7..d49903fc14 100644 --- a/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/JsonUtils.java +++ b/framework/sdk-parent/jmeter/src/main/java/io/metersphere/utils/JsonUtils.java @@ -18,7 +18,7 @@ import java.util.List; public class JsonUtils { private static final ObjectMapper objectMapper = new ObjectMapper(); - public static final int DEFAULT_MAX_STRING_LEN = 20_000_000_0; + public static final int DEFAULT_MAX_STRING_LEN = Integer.MAX_VALUE; static { objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/autoconfigure/RestTemplateConfig.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/autoconfigure/RestTemplateConfig.java index 43b782e73e..d46e84920c 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/autoconfigure/RestTemplateConfig.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/autoconfigure/RestTemplateConfig.java @@ -25,11 +25,14 @@ import org.apache.hc.core5.http.io.SocketConfig; import org.apache.hc.core5.http.protocol.HttpContext; import org.springframework.context.annotation.Bean; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; import java.io.IOException; import java.net.ConnectException; import java.net.SocketException; +import java.util.List; import java.util.concurrent.TimeUnit; @@ -43,13 +46,13 @@ public class RestTemplateConfig { private final static int SOCKET_TIMEOUT = 200 * 1000; @Bean - public RestTemplate restTemplate() { - return setTemplate(); + public RestTemplate restTemplate(ObjectMapper objectMapper, HttpClient httpClient) { + return setTemplate(objectMapper, httpClient); } @Bean - public RestTemplate restTemplateWithTimeOut() { - return setTemplate(); + public RestTemplate restTemplateWithTimeOut(ObjectMapper objectMapper, HttpClient httpClient) { + return setTemplate(objectMapper, httpClient); } @Bean @@ -89,9 +92,23 @@ public class RestTemplateConfig { return mapper; } - private RestTemplate setTemplate() { + public RestTemplate setTemplate(ObjectMapper objectMapper, HttpClient httpClient) { RestTemplate restTemplate = new RestTemplate(); - restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient())); + restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)); + + List> messageConverters = restTemplate.getMessageConverters(); + for (int i = 0; i < messageConverters.size(); i++) { + if (messageConverters.get(i) instanceof MappingJackson2HttpMessageConverter) { + // 使用自定义ObjectMapper创建HttpMessageConverter + MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); + converter.setObjectMapper(objectMapper); + messageConverters.set(i, converter); + } + } + + // 将HttpMessageConverter加入到RestTemplate中 + restTemplate.setMessageConverters(messageConverters); + return restTemplate; }