2026-03-02 09:59:10 +08:00
|
|
|
|
package com.ruoyi.lsfx.util;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.http.*;
|
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
2026-03-02 10:17:24 +08:00
|
|
|
|
import jakarta.annotation.Resource;
|
2026-03-02 09:59:10 +08:00
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* HTTP请求工具类
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Component
|
|
|
|
|
|
public class HttpUtil {
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 发送GET请求(带请求头)
|
|
|
|
|
|
* @param url 请求URL
|
|
|
|
|
|
* @param headers 请求头
|
|
|
|
|
|
* @param responseType 响应类型
|
|
|
|
|
|
* @return 响应对象
|
|
|
|
|
|
*/
|
|
|
|
|
|
public <T> T get(String url, Map<String, String> headers, Class<T> responseType) {
|
|
|
|
|
|
HttpHeaders httpHeaders = createHeaders(headers);
|
|
|
|
|
|
HttpEntity<Void> requestEntity = new HttpEntity<>(httpHeaders);
|
|
|
|
|
|
|
|
|
|
|
|
ResponseEntity<T> response = restTemplate.exchange(
|
|
|
|
|
|
url, HttpMethod.GET, requestEntity, responseType
|
|
|
|
|
|
);
|
|
|
|
|
|
return response.getBody();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 发送POST请求(JSON格式,带请求头)
|
|
|
|
|
|
* @param url 请求URL
|
|
|
|
|
|
* @param request 请求对象
|
|
|
|
|
|
* @param headers 请求头
|
|
|
|
|
|
* @param responseType 响应类型
|
|
|
|
|
|
* @return 响应对象
|
|
|
|
|
|
*/
|
|
|
|
|
|
public <T> T postJson(String url, Object request, Map<String, String> headers, Class<T> responseType) {
|
|
|
|
|
|
HttpHeaders httpHeaders = createHeaders(headers);
|
|
|
|
|
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
|
|
|
|
|
|
|
HttpEntity<Object> requestEntity = new HttpEntity<>(request, httpHeaders);
|
|
|
|
|
|
|
|
|
|
|
|
ResponseEntity<T> response = restTemplate.postForEntity(url, requestEntity, responseType);
|
|
|
|
|
|
return response.getBody();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 上传文件(Multipart格式)
|
|
|
|
|
|
* @param url 请求URL
|
|
|
|
|
|
* @param params 参数(包含文件)
|
|
|
|
|
|
* @param headers 请求头
|
|
|
|
|
|
* @param responseType 响应类型
|
|
|
|
|
|
* @return 响应对象
|
|
|
|
|
|
*/
|
|
|
|
|
|
public <T> T uploadFile(String url, Map<String, Object> params, Map<String, String> headers, Class<T> responseType) {
|
|
|
|
|
|
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);
|
|
|
|
|
|
return response.getBody();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建请求头
|
|
|
|
|
|
* @param headers 请求头Map
|
|
|
|
|
|
* @return HttpHeaders对象
|
|
|
|
|
|
*/
|
|
|
|
|
|
private HttpHeaders createHeaders(Map<String, String> headers) {
|
|
|
|
|
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
|
|
|
if (headers != null && !headers.isEmpty()) {
|
|
|
|
|
|
headers.forEach(httpHeaders::set);
|
|
|
|
|
|
}
|
|
|
|
|
|
return httpHeaders;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|