lsfx请求方式修改
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.ruoyi.lsfx.util;
|
||||
|
||||
import com.ruoyi.lsfx.exception.LsfxApiException;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
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.RestTemplate;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
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格式)
|
||||
* @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