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; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import java.util.Map; /** * HTTP请求工具类 */ @Component public class HttpUtil { @Resource private RestTemplate restTemplate; /** * 发送GET请求(带请求头) * @param url 请求URL * @param headers 请求头 * @param responseType 响应类型 * @return 响应对象 */ public T get(String url, Map headers, Class responseType) { try { HttpHeaders httpHeaders = createHeaders(headers); HttpEntity requestEntity = new HttpEntity<>(httpHeaders); ResponseEntity response = restTemplate.exchange( url, HttpMethod.GET, requestEntity, responseType ); if (!response.getStatusCode().is2xxSuccessful()) { throw new LsfxApiException("API调用失败,HTTP状态码: " + response.getStatusCode()); } T body = response.getBody(); if (body == null) { throw new LsfxApiException("API返回数据为空"); } return body; } catch (RestClientException e) { throw new LsfxApiException("网络请求失败: " + e.getMessage(), e); } } /** * 发送POST请求(JSON格式,带请求头) * @param url 请求URL * @param request 请求对象 * @param headers 请求头 * @param responseType 响应类型 * @return 响应对象 */ public T postJson(String url, Object request, Map headers, Class responseType) { try { HttpHeaders httpHeaders = createHeaders(headers); httpHeaders.setContentType(MediaType.APPLICATION_JSON); HttpEntity requestEntity = new HttpEntity<>(request, httpHeaders); ResponseEntity response = restTemplate.postForEntity(url, requestEntity, responseType); if (!response.getStatusCode().is2xxSuccessful()) { throw new LsfxApiException("API调用失败,HTTP状态码: " + response.getStatusCode()); } T body = response.getBody(); if (body == null) { throw new LsfxApiException("API返回数据为空"); } return body; } catch (RestClientException e) { throw new LsfxApiException("网络请求失败: " + e.getMessage(), e); } } /** * 发送POST请求(multipart/form-data格式,带请求头) * 用于提交表单数据(非文件上传场景) * @param url 请求URL * @param params 表单参数 * @param headers 请求头 * @param responseType 响应类型 * @return 响应对象 */ public T postFormData(String url, Map params, Map headers, Class responseType) { try { HttpHeaders httpHeaders = createHeaders(headers); httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap body = new LinkedMultiValueMap<>(); if (params != null) { params.forEach(body::add); } HttpEntity> requestEntity = new HttpEntity<>(body, httpHeaders); ResponseEntity 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 * @param params 参数(包含文件) * @param headers 请求头 * @param responseType 响应类型 * @return 响应对象 */ public T uploadFile(String url, Map params, Map headers, Class responseType) { try { HttpHeaders httpHeaders = createHeaders(headers); httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap body = new LinkedMultiValueMap<>(); if (params != null) { params.forEach(body::add); } HttpEntity> requestEntity = new HttpEntity<>(body, httpHeaders); ResponseEntity response = restTemplate.postForEntity(url, requestEntity, responseType); if (!response.getStatusCode().is2xxSuccessful()) { throw new LsfxApiException("文件上传失败,HTTP状态码: " + response.getStatusCode()); } T responseBody = response.getBody(); if (responseBody == null) { throw new LsfxApiException("文件上传返回数据为空"); } return responseBody; } catch (RestClientException e) { throw new LsfxApiException("文件上传请求失败: " + e.getMessage(), e); } } /** * 创建请求头 * @param headers 请求头Map * @return HttpHeaders对象 */ private HttpHeaders createHeaders(Map headers) { HttpHeaders httpHeaders = new HttpHeaders(); if (headers != null && !headers.isEmpty()) { headers.forEach(httpHeaders::set); } return httpHeaders; } }