模型调用

This commit is contained in:
wkc
2026-01-21 15:58:39 +08:00
parent c4a05e1338
commit 0d061155ed
25 changed files with 1134 additions and 55 deletions

View File

@@ -11,6 +11,8 @@ import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
@@ -21,7 +23,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
/**
* 通用http发送方法
@@ -290,4 +295,79 @@ public class HttpUtils
return true;
}
}
/**
* 发送 POST 请求application/x-www-form-urlencoded 格式)
* @param url 请求地址
* @param params 表单参数
* @param headers 请求头(可为 null
* @param responseType 响应类型
* @param <T> 泛型返回值
* @return 响应结果
*/
public static <T> T doPostFormUrlEncoded(String url, Map<String, String> params, HttpHeaders headers, Class<T> responseType) {
// 构建表单参数
MultiValueMap<String, String> formParams = new LinkedMultiValueMap<>();
if (params != null && !params.isEmpty()) {
formParams.setAll(params);
}
// 构建请求头(指定 Content-Type
HttpHeaders requestHeaders = headers == null ? new HttpHeaders() : headers;
requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
requestHeaders.setAcceptCharset(Collections.singletonList(StandardCharsets.UTF_8));
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formParams, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
// 发送请求
try {
ResponseEntity<T> response = restTemplate.exchange(
url,
HttpMethod.POST,
requestEntity,
responseType
);
log.info("---------------------->POST(form-urlencoded) 请求成功URL{},响应结果:{}", url, response.getBody());
return response.getBody();
} catch (Exception e) {
throw new RuntimeException("POST(form-urlencoded) 请求失败URL" + url + ",异常信息:" + e.getMessage(), e);
}
}
// ========== 新增JSON 格式 POST 请求 ==========
/**
* 发送 POST 请求application/json 格式)
* @param url 请求地址
* @param requestBody 请求体Java 对象,自动序列化为 JSON
* @param headers 请求头(可为 null默认已设置 Content-Type: application/json
* @param responseType 响应类型
* @param <T> 响应泛型类型
* @param <R> 请求体类型
* @return 响应结果
*/
public static <T, R> T doPostJson(String url, R requestBody, HttpHeaders headers, Class<T> responseType) {
// 构建请求头,默认设置 JSON 格式
HttpHeaders requestHeaders = headers == null ? new HttpHeaders() : headers;
// 确保 Content-Type 为 JSON避免外部传入覆盖
if (!requestHeaders.containsKey(HttpHeaders.CONTENT_TYPE)) {
requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
}
// 构建请求实体(请求体 + 请求头)
HttpEntity<R> requestEntity = new HttpEntity<>(requestBody, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
// 发送 JSON POST 请求
try {
ResponseEntity<T> response = restTemplate.exchange(
url,
HttpMethod.POST,
requestEntity,
responseType
);
log.info("---------------------->POST(JSON) 请求成功URL{},响应结果:{}", url, response.getBody());
return response.getBody();
} catch (Exception e) {
throw new RuntimeException("POST(JSON) 请求失败URL" + url + ",异常信息:" + e.getMessage(), e);
}
}
}