From a7c46152271200314567e76a1ea1ce62636b9186 Mon Sep 17 00:00:00 2001 From: wzy-warehouse <18135009705@163.com> Date: Thu, 18 Jun 2026 18:08:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=A0=E9=99=A4RestClientConfig+?= =?UTF-8?q?HttpRequestClientUtils=EF=BC=8CFeignServiceImpl=E6=94=B9?= =?UTF-8?q?=E7=94=A8Spring=20Boot=E5=86=85=E7=BD=AERestClient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gis/xian/config/RestClientConfig.java | 29 --- .../qgis/base/impl/FeignServiceImpl.java | 15 +- .../xian/utils/HttpRequestClientUtils.java | 197 ------------------ 3 files changed, 10 insertions(+), 231 deletions(-) delete mode 100644 src/main/java/com/gis/xian/config/RestClientConfig.java delete mode 100644 src/main/java/com/gis/xian/utils/HttpRequestClientUtils.java diff --git a/src/main/java/com/gis/xian/config/RestClientConfig.java b/src/main/java/com/gis/xian/config/RestClientConfig.java deleted file mode 100644 index 394f1b1..0000000 --- a/src/main/java/com/gis/xian/config/RestClientConfig.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.gis.xian.config; - -import com.gis.xian.utils.HttpRequestClientUtils; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.client.RestTemplate; - -/** - * @author zzw - * @description: 第三方请求配置类 - * @date 2026/5/26 下午6:40 - */ -@Configuration -public class RestClientConfig { - - @Bean - public RestTemplate restTemplate() { - return new RestTemplate(); - } - - @Bean - public HttpRequestClientUtils httpRestClient(RestTemplate restTemplate) { - HttpRequestClientUtils client = new HttpRequestClientUtils(restTemplate); - // 添加全局默认请求头 - // client.addDefaultHeader("X-App-Id", "my-app-id"); - return client; - } -} - diff --git a/src/main/java/com/gis/xian/service/qgis/base/impl/FeignServiceImpl.java b/src/main/java/com/gis/xian/service/qgis/base/impl/FeignServiceImpl.java index d11f55f..7a05de6 100644 --- a/src/main/java/com/gis/xian/service/qgis/base/impl/FeignServiceImpl.java +++ b/src/main/java/com/gis/xian/service/qgis/base/impl/FeignServiceImpl.java @@ -3,12 +3,12 @@ package com.gis.xian.service.qgis.base.impl; import com.alibaba.fastjson2.JSON; import com.gis.xian.dto.qgis.base.QgisTriggerDTO; import com.gis.xian.service.qgis.base.IFeignService; -import com.gis.xian.utils.HttpRequestClientUtils; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.MediaType; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestClient; /** * 统一专题图触发服务 @@ -19,7 +19,7 @@ import org.springframework.stereotype.Service; public class FeignServiceImpl implements IFeignService { @Resource - private HttpRequestClientUtils restclient; + private RestClient.Builder restClientBuilder; @Value("${qgis.url}") private String qgisUrl; @@ -35,8 +35,13 @@ public class FeignServiceImpl implements IFeignService { log.info("触发专题图生成: simulationId={}", triggerDTO.getSimulationId()); try { - ParameterizedTypeReference res = new ParameterizedTypeReference() {}; - String result = restclient.post(qgisUrl, JSON.toJSON(triggerDTO), res); + RestClient client = restClientBuilder.build(); + String result = client.post() + .uri(qgisUrl) + .contentType(MediaType.APPLICATION_JSON) + .body(JSON.toJSON(triggerDTO)) + .retrieve() + .body(String.class); log.info("Python 端响应: {}", result); } catch (Exception e) { log.error("调用 Python QGIS 服务失败: {}", e.getMessage(), e); diff --git a/src/main/java/com/gis/xian/utils/HttpRequestClientUtils.java b/src/main/java/com/gis/xian/utils/HttpRequestClientUtils.java deleted file mode 100644 index 6c94eed..0000000 --- a/src/main/java/com/gis/xian/utils/HttpRequestClientUtils.java +++ /dev/null @@ -1,197 +0,0 @@ -package com.gis.xian.utils; - -import org.springframework.core.ParameterizedTypeReference; -import org.springframework.http.*; -import org.springframework.web.client.RestTemplate; - -import java.util.Map; - -/** - * @author zzw - * @description: 第三方请求工具类 - * @date 2026/5/26 下午4:27 - */ -public class HttpRequestClientUtils { - - private final RestTemplate restTemplate; - - // 默认的请求头设置 - private static final HttpHeaders defaultHeaders; - - static { - defaultHeaders = new HttpHeaders(); - defaultHeaders.setContentType(MediaType.APPLICATION_JSON); - defaultHeaders.add("Accept", "application/json"); - } - - /** - * 构造方法,注入RestTemplate - * @param restTemplate RestTemplate实例 - */ - public HttpRequestClientUtils(RestTemplate restTemplate) { - this.restTemplate = restTemplate; - } - - /** - * 不带Token的GET请求 - * @param url 请求URL - * @param responseType 响应数据类型 - * @param 响应数据泛型 - * @return 响应结果 - */ - public T get(String url, ParameterizedTypeReference responseType) { - return get(url, null, responseType); - } - - /** - * 带Token的GET请求 - * @param url 请求URL - * @param token 认证Token - * @param responseType 响应数据类型 - * @param 响应数据泛型 - * @return 响应结果 - */ - public T get(String url, String token, ParameterizedTypeReference responseType) { - HttpEntity entity = createHttpEntity(token, null); - ResponseEntity response = restTemplate.exchange( - url, - HttpMethod.GET, - entity, - responseType - ); - return response.getBody(); - } - - /** - * 带路径参数和Token的GET请求 - * @param url 请求URL,包含路径参数占位符,如"/user/{id}" - * @param token 认证Token - * @param uriVariables 路径参数键值对 - * @param responseType 响应数据类型 - * @param 响应数据泛型 - * @return 响应结果 - */ - public T get(String url, String token, Map uriVariables, ParameterizedTypeReference responseType) { - HttpEntity entity = createHttpEntity(token, null); - ResponseEntity response = restTemplate.exchange( - url, - HttpMethod.GET, - entity, - responseType, - uriVariables - ); - return response.getBody(); - } - - /** - * 不带Token的POST请求 - * @param url 请求URL - * @param requestBody 请求体 - * @param responseType 响应数据类型 - * @param 响应数据泛型 - * @param 请求体泛型 - * @return 响应结果 - */ - public T post(String url, R requestBody, ParameterizedTypeReference responseType) { - return post(url, null, requestBody, responseType); - } - - /** - * 带Token的POST请求 - * @param url 请求URL - * @param token 认证Token - * @param requestBody 请求体 - * @param responseType 响应数据类型 - * @param 响应数据泛型 - * @param 请求体泛型 - * @return 响应结果 - */ - public T post(String url, String token, R requestBody, ParameterizedTypeReference responseType) { - HttpEntity entity = createHttpEntity(token, requestBody); - ResponseEntity response = restTemplate.exchange( - url, - HttpMethod.POST, - entity, - responseType - ); - return response.getBody(); - } - - /** - * 带Token的PUT请求 - * @param url 请求URL - * @param token 认证Token - * @param requestBody 请求体 - * @param responseType 响应数据类型 - * @param 响应数据泛型 - * @param 请求体泛型 - * @return 响应结果 - */ - public T put(String url, String token, R requestBody, ParameterizedTypeReference responseType) { - HttpEntity entity = createHttpEntity(token, requestBody); - ResponseEntity response = restTemplate.exchange( - url, - HttpMethod.PUT, - entity, - responseType - ); - return response.getBody(); - } - - /** - * 带Token的DELETE请求 - * @param url 请求URL - * @param token 认证Token - * @param responseType 响应数据类型 - * @param 响应数据泛型 - * @return 响应结果 - */ - public T delete(String url, String token, ParameterizedTypeReference responseType) { - HttpEntity entity = createHttpEntity(token, null); - ResponseEntity response = restTemplate.exchange( - url, - HttpMethod.DELETE, - entity, - responseType - ); - return response.getBody(); - } - - /** - * 创建HTTP请求实体,包含 headers 和 body - * @param token 认证Token,可为null - * @param body 请求体,可为null - * @param 请求体泛型 - * @return HttpEntity实例 - */ - private HttpEntity createHttpEntity(String token, R body) { - HttpHeaders headers = new HttpHeaders(); - // 复制默认请求头 - headers.putAll(defaultHeaders); - - // 如果有token,添加到请求头 - if (token != null && !token.trim().isEmpty()) { - headers.add("Authorization", "Bearer " + token); - } - - return new HttpEntity<>(body, headers); - } - - /** - * 添加默认请求头 - * @param name 头名称 - * @param value 头值 - */ - public void addDefaultHeader(String name, String value) { - defaultHeaders.add(name, value); - } - - /** - * 移除默认请求头 - * @param name 头名称 - */ - public void removeDefaultHeader(String name) { - defaultHeaders.remove(name); - } -} -