lsfx请求方式修改
This commit is contained in:
@@ -1,16 +1,19 @@
|
|||||||
package com.ruoyi.lsfx.client;
|
package com.ruoyi.lsfx.client;
|
||||||
|
|
||||||
import com.ruoyi.lsfx.constants.LsfxConstants;
|
import com.ruoyi.lsfx.constants.LsfxConstants;
|
||||||
import com.ruoyi.lsfx.domain.request.*;
|
import com.ruoyi.lsfx.domain.request.FetchInnerFlowRequest;
|
||||||
|
import com.ruoyi.lsfx.domain.request.GetBankStatementRequest;
|
||||||
|
import com.ruoyi.lsfx.domain.request.GetTokenRequest;
|
||||||
import com.ruoyi.lsfx.domain.response.*;
|
import com.ruoyi.lsfx.domain.response.*;
|
||||||
import com.ruoyi.lsfx.exception.LsfxApiException;
|
import com.ruoyi.lsfx.exception.LsfxApiException;
|
||||||
import com.ruoyi.lsfx.util.HttpUtil;
|
import com.ruoyi.lsfx.util.HttpUtil;
|
||||||
import com.ruoyi.lsfx.util.MD5Util;
|
import com.ruoyi.lsfx.util.MD5Util;
|
||||||
|
import com.ruoyi.lsfx.util.ObjectUtil;
|
||||||
|
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.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -64,18 +67,17 @@ public class LsfxAnalysisClient {
|
|||||||
request.getEntityName(),
|
request.getEntityName(),
|
||||||
appSecret
|
appSecret
|
||||||
);
|
);
|
||||||
request.setAppSecretCode(secretCode);
|
|
||||||
request.setAppId(appId);
|
|
||||||
|
|
||||||
if (request.getAnalysisType() == null) {
|
// 构建form-data参数(使用ObjectUtil简化代码)
|
||||||
request.setAnalysisType(LsfxConstants.ANALYSIS_TYPE);
|
Map<String, Object> params = ObjectUtil.toMapIgnoreNull(request);
|
||||||
}
|
// 添加特殊字段
|
||||||
if (request.getRole() == null) {
|
params.put("appId", appId);
|
||||||
request.setRole(LsfxConstants.DEFAULT_ROLE);
|
params.put("appSecretCode", secretCode);
|
||||||
}
|
params.put("role", request.getRole() != null ? request.getRole() : LsfxConstants.DEFAULT_ROLE);
|
||||||
|
params.put("analysisType", request.getAnalysisType() != null ? request.getAnalysisType() : LsfxConstants.ANALYSIS_TYPE);
|
||||||
|
|
||||||
String url = baseUrl + getTokenEndpoint;
|
String url = baseUrl + getTokenEndpoint;
|
||||||
GetTokenResponse response = httpUtil.postJson(url, request, null, GetTokenResponse.class);
|
GetTokenResponse response = httpUtil.postFormData(url, params, null, GetTokenResponse.class);
|
||||||
|
|
||||||
long elapsed = System.currentTimeMillis() - startTime;
|
long elapsed = System.currentTimeMillis() - startTime;
|
||||||
if (response != null && response.getData() != null) {
|
if (response != null && response.getData() != null) {
|
||||||
@@ -142,10 +144,13 @@ public class LsfxAnalysisClient {
|
|||||||
try {
|
try {
|
||||||
String url = baseUrl + fetchInnerFlowEndpoint;
|
String url = baseUrl + fetchInnerFlowEndpoint;
|
||||||
|
|
||||||
|
// 构建form-data参数(使用ObjectUtil简化代码)
|
||||||
|
Map<String, Object> params = ObjectUtil.toMapIgnoreNull(request);
|
||||||
|
|
||||||
Map<String, String> headers = new HashMap<>();
|
Map<String, String> headers = new HashMap<>();
|
||||||
headers.put(LsfxConstants.HEADER_CLIENT_ID, clientId);
|
headers.put(LsfxConstants.HEADER_CLIENT_ID, clientId);
|
||||||
|
|
||||||
FetchInnerFlowResponse response = httpUtil.postJson(url, request, headers, FetchInnerFlowResponse.class);
|
FetchInnerFlowResponse response = httpUtil.postFormData(url, params, headers, FetchInnerFlowResponse.class);
|
||||||
|
|
||||||
long elapsed = System.currentTimeMillis() - startTime;
|
long elapsed = System.currentTimeMillis() - startTime;
|
||||||
if (response != null && response.getData() != null) {
|
if (response != null && response.getData() != null) {
|
||||||
@@ -175,6 +180,7 @@ public class LsfxAnalysisClient {
|
|||||||
try {
|
try {
|
||||||
String url = baseUrl + checkParseStatusEndpoint;
|
String url = baseUrl + checkParseStatusEndpoint;
|
||||||
|
|
||||||
|
// 构建form-data参数
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
params.put("groupId", groupId);
|
params.put("groupId", groupId);
|
||||||
params.put("inprogressList", inprogressList);
|
params.put("inprogressList", inprogressList);
|
||||||
@@ -182,7 +188,7 @@ public class LsfxAnalysisClient {
|
|||||||
Map<String, String> headers = new HashMap<>();
|
Map<String, String> headers = new HashMap<>();
|
||||||
headers.put(LsfxConstants.HEADER_CLIENT_ID, clientId);
|
headers.put(LsfxConstants.HEADER_CLIENT_ID, clientId);
|
||||||
|
|
||||||
CheckParseStatusResponse response = httpUtil.postJson(url, params, headers, CheckParseStatusResponse.class);
|
CheckParseStatusResponse response = httpUtil.postFormData(url, params, headers, CheckParseStatusResponse.class);
|
||||||
|
|
||||||
long elapsed = System.currentTimeMillis() - startTime;
|
long elapsed = System.currentTimeMillis() - startTime;
|
||||||
if (response != null && response.getData() != null) {
|
if (response != null && response.getData() != null) {
|
||||||
@@ -219,10 +225,13 @@ public class LsfxAnalysisClient {
|
|||||||
try {
|
try {
|
||||||
String url = baseUrl + getBankStatementEndpoint;
|
String url = baseUrl + getBankStatementEndpoint;
|
||||||
|
|
||||||
|
// 构建form-data参数(使用ObjectUtil简化代码)
|
||||||
|
Map<String, Object> params = ObjectUtil.toMapIgnoreNull(request);
|
||||||
|
|
||||||
Map<String, String> headers = new HashMap<>();
|
Map<String, String> headers = new HashMap<>();
|
||||||
headers.put(LsfxConstants.HEADER_CLIENT_ID, clientId);
|
headers.put(LsfxConstants.HEADER_CLIENT_ID, clientId);
|
||||||
|
|
||||||
GetBankStatementResponse response = httpUtil.postJson(url, request, headers, GetBankStatementResponse.class);
|
GetBankStatementResponse response = httpUtil.postFormData(url, params, headers, GetBankStatementResponse.class);
|
||||||
|
|
||||||
long elapsed = System.currentTimeMillis() - startTime;
|
long elapsed = System.currentTimeMillis() - startTime;
|
||||||
if (response != null && response.getData() != null) {
|
if (response != null && response.getData() != null) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.ruoyi.lsfx.util;
|
package com.ruoyi.lsfx.util;
|
||||||
|
|
||||||
import com.ruoyi.lsfx.exception.LsfxApiException;
|
import com.ruoyi.lsfx.exception.LsfxApiException;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
@@ -8,7 +9,6 @@ import org.springframework.util.MultiValueMap;
|
|||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -83,6 +83,44 @@ public class HttpUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送POST请求(multipart/form-data格式,带请求头)
|
||||||
|
* 用于提交表单数据(非文件上传场景)
|
||||||
|
* @param url 请求URL
|
||||||
|
* @param params 表单参数
|
||||||
|
* @param headers 请求头
|
||||||
|
* @param responseType 响应类型
|
||||||
|
* @return 响应对象
|
||||||
|
*/
|
||||||
|
public <T> T postFormData(String url, Map<String, Object> params, Map<String, String> headers, Class<T> responseType) {
|
||||||
|
try {
|
||||||
|
HttpHeaders httpHeaders = createHeaders(headers);
|
||||||
|
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||||
|
|
||||||
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||||
|
if (params != null) {
|
||||||
|
params.forEach(body::add);
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, httpHeaders);
|
||||||
|
|
||||||
|
ResponseEntity<T> response = restTemplate.postForEntity(url, requestEntity, responseType);
|
||||||
|
|
||||||
|
if (!response.getStatusCode().is2xxSuccessful()) {
|
||||||
|
throw new LsfxApiException("API调用失败,HTTP状态码: " + response.getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
T responseBody = response.getBody();
|
||||||
|
if (responseBody == null) {
|
||||||
|
throw new LsfxApiException("API返回数据为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseBody;
|
||||||
|
} catch (RestClientException e) {
|
||||||
|
throw new LsfxApiException("网络请求失败: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传文件(Multipart格式)
|
* 上传文件(Multipart格式)
|
||||||
* @param url 请求URL
|
* @param url 请求URL
|
||||||
|
|||||||
65
ccdi-lsfx/src/main/java/com/ruoyi/lsfx/util/ObjectUtil.java
Normal file
65
ccdi-lsfx/src/main/java/com/ruoyi/lsfx/util/ObjectUtil.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package com.ruoyi.lsfx.util;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对象转换工具类
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class ObjectUtil {
|
||||||
|
|
||||||
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将对象转换为Map
|
||||||
|
* 使用Jackson ObjectMapper进行转换,支持复杂对象和嵌套结构
|
||||||
|
*
|
||||||
|
* @param obj 要转换的对象
|
||||||
|
* @return 转换后的Map,如果转换失败返回空Map
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static Map<String, Object> toMap(Object obj) {
|
||||||
|
if (obj == null) {
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 先将对象转为JSON字符串,再转为Map
|
||||||
|
// 这种方式可以处理复杂对象、嵌套对象、集合等
|
||||||
|
String json = OBJECT_MAPPER.writeValueAsString(obj);
|
||||||
|
return OBJECT_MAPPER.readValue(json, Map.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("对象转Map失败: {}", e.getMessage(), e);
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将对象转换为Map(忽略空值)
|
||||||
|
*
|
||||||
|
* @param obj 要转换的对象
|
||||||
|
* @return 转换后的Map(不包含null值),如果转换失败返回空Map
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static Map<String, Object> toMapIgnoreNull(Object obj) {
|
||||||
|
if (obj == null) {
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 配置ObjectMapper在序列化时忽略空值
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL);
|
||||||
|
|
||||||
|
String json = mapper.writeValueAsString(obj);
|
||||||
|
return mapper.readValue(json, Map.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("对象转Map失败: {}", e.getMessage(), e);
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user