refactor: 删除RestClientConfig+HttpRequestClientUtils,FeignServiceImpl改用Spring Boot内置RestClient
This commit is contained in:
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -3,12 +3,12 @@ package com.gis.xian.service.qgis.base.impl;
|
|||||||
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSON;
|
||||||
import com.gis.xian.dto.qgis.base.QgisTriggerDTO;
|
import com.gis.xian.dto.qgis.base.QgisTriggerDTO;
|
||||||
import com.gis.xian.service.qgis.base.IFeignService;
|
import com.gis.xian.service.qgis.base.IFeignService;
|
||||||
import com.gis.xian.utils.HttpRequestClientUtils;
|
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
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.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 统一专题图触发服务
|
* 统一专题图触发服务
|
||||||
@@ -19,7 +19,7 @@ import org.springframework.stereotype.Service;
|
|||||||
public class FeignServiceImpl implements IFeignService {
|
public class FeignServiceImpl implements IFeignService {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private HttpRequestClientUtils restclient;
|
private RestClient.Builder restClientBuilder;
|
||||||
|
|
||||||
@Value("${qgis.url}")
|
@Value("${qgis.url}")
|
||||||
private String qgisUrl;
|
private String qgisUrl;
|
||||||
@@ -35,8 +35,13 @@ public class FeignServiceImpl implements IFeignService {
|
|||||||
log.info("触发专题图生成: simulationId={}", triggerDTO.getSimulationId());
|
log.info("触发专题图生成: simulationId={}", triggerDTO.getSimulationId());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ParameterizedTypeReference<String> res = new ParameterizedTypeReference<String>() {};
|
RestClient client = restClientBuilder.build();
|
||||||
String result = restclient.post(qgisUrl, JSON.toJSON(triggerDTO), res);
|
String result = client.post()
|
||||||
|
.uri(qgisUrl)
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.body(JSON.toJSON(triggerDTO))
|
||||||
|
.retrieve()
|
||||||
|
.body(String.class);
|
||||||
log.info("Python 端响应: {}", result);
|
log.info("Python 端响应: {}", result);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("调用 Python QGIS 服务失败: {}", e.getMessage(), e);
|
log.error("调用 Python QGIS 服务失败: {}", e.getMessage(), e);
|
||||||
|
|||||||
@@ -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 <T> 响应数据泛型
|
|
||||||
* @return 响应结果
|
|
||||||
*/
|
|
||||||
public <T> T get(String url, ParameterizedTypeReference<T> responseType) {
|
|
||||||
return get(url, null, responseType);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 带Token的GET请求
|
|
||||||
* @param url 请求URL
|
|
||||||
* @param token 认证Token
|
|
||||||
* @param responseType 响应数据类型
|
|
||||||
* @param <T> 响应数据泛型
|
|
||||||
* @return 响应结果
|
|
||||||
*/
|
|
||||||
public <T> T get(String url, String token, ParameterizedTypeReference<T> responseType) {
|
|
||||||
HttpEntity<?> entity = createHttpEntity(token, null);
|
|
||||||
ResponseEntity<T> 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 <T> 响应数据泛型
|
|
||||||
* @return 响应结果
|
|
||||||
*/
|
|
||||||
public <T> T get(String url, String token, Map<String, ?> uriVariables, ParameterizedTypeReference<T> responseType) {
|
|
||||||
HttpEntity<?> entity = createHttpEntity(token, null);
|
|
||||||
ResponseEntity<T> response = restTemplate.exchange(
|
|
||||||
url,
|
|
||||||
HttpMethod.GET,
|
|
||||||
entity,
|
|
||||||
responseType,
|
|
||||||
uriVariables
|
|
||||||
);
|
|
||||||
return response.getBody();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 不带Token的POST请求
|
|
||||||
* @param url 请求URL
|
|
||||||
* @param requestBody 请求体
|
|
||||||
* @param responseType 响应数据类型
|
|
||||||
* @param <T> 响应数据泛型
|
|
||||||
* @param <R> 请求体泛型
|
|
||||||
* @return 响应结果
|
|
||||||
*/
|
|
||||||
public <T, R> T post(String url, R requestBody, ParameterizedTypeReference<T> responseType) {
|
|
||||||
return post(url, null, requestBody, responseType);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 带Token的POST请求
|
|
||||||
* @param url 请求URL
|
|
||||||
* @param token 认证Token
|
|
||||||
* @param requestBody 请求体
|
|
||||||
* @param responseType 响应数据类型
|
|
||||||
* @param <T> 响应数据泛型
|
|
||||||
* @param <R> 请求体泛型
|
|
||||||
* @return 响应结果
|
|
||||||
*/
|
|
||||||
public <T, R> T post(String url, String token, R requestBody, ParameterizedTypeReference<T> responseType) {
|
|
||||||
HttpEntity<R> entity = createHttpEntity(token, requestBody);
|
|
||||||
ResponseEntity<T> response = restTemplate.exchange(
|
|
||||||
url,
|
|
||||||
HttpMethod.POST,
|
|
||||||
entity,
|
|
||||||
responseType
|
|
||||||
);
|
|
||||||
return response.getBody();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 带Token的PUT请求
|
|
||||||
* @param url 请求URL
|
|
||||||
* @param token 认证Token
|
|
||||||
* @param requestBody 请求体
|
|
||||||
* @param responseType 响应数据类型
|
|
||||||
* @param <T> 响应数据泛型
|
|
||||||
* @param <R> 请求体泛型
|
|
||||||
* @return 响应结果
|
|
||||||
*/
|
|
||||||
public <T, R> T put(String url, String token, R requestBody, ParameterizedTypeReference<T> responseType) {
|
|
||||||
HttpEntity<R> entity = createHttpEntity(token, requestBody);
|
|
||||||
ResponseEntity<T> response = restTemplate.exchange(
|
|
||||||
url,
|
|
||||||
HttpMethod.PUT,
|
|
||||||
entity,
|
|
||||||
responseType
|
|
||||||
);
|
|
||||||
return response.getBody();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 带Token的DELETE请求
|
|
||||||
* @param url 请求URL
|
|
||||||
* @param token 认证Token
|
|
||||||
* @param responseType 响应数据类型
|
|
||||||
* @param <T> 响应数据泛型
|
|
||||||
* @return 响应结果
|
|
||||||
*/
|
|
||||||
public <T> T delete(String url, String token, ParameterizedTypeReference<T> responseType) {
|
|
||||||
HttpEntity<?> entity = createHttpEntity(token, null);
|
|
||||||
ResponseEntity<T> response = restTemplate.exchange(
|
|
||||||
url,
|
|
||||||
HttpMethod.DELETE,
|
|
||||||
entity,
|
|
||||||
responseType
|
|
||||||
);
|
|
||||||
return response.getBody();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建HTTP请求实体,包含 headers 和 body
|
|
||||||
* @param token 认证Token,可为null
|
|
||||||
* @param body 请求体,可为null
|
|
||||||
* @param <R> 请求体泛型
|
|
||||||
* @return HttpEntity实例
|
|
||||||
*/
|
|
||||||
private <R> HttpEntity<R> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user