This commit is contained in:
xy
2026-01-26 19:30:33 +08:00
parent 4edb27d20b
commit 2a5a5087ad

View File

@@ -1,9 +1,9 @@
package com.njcn.web.utils; package com.njcn.web.utils;
import org.springframework.http.HttpEntity; import org.slf4j.Logger;
import org.springframework.http.HttpHeaders; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod; import org.springframework.http.*;
import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.net.URI; import java.net.URI;
@@ -15,8 +15,11 @@ import java.util.Map;
* @createDate 2019-02-08 * @createDate 2019-02-08
* *
*/ */
@Component
public class RestTemplateUtil { public class RestTemplateUtil {
private static final Logger log = LoggerFactory.getLogger(RestTemplateUtil.class);
private static final RestTemplate restTemplate = new RestTemplate(); private static final RestTemplate restTemplate = new RestTemplate();
// ----------------------------------GET------------------------------------------------------- // ----------------------------------GET-------------------------------------------------------
@@ -263,6 +266,24 @@ public class RestTemplateUtil {
return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables); return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables);
} }
public <T> ResponseEntity<T> post(String url, Object requestBody, HttpHeaders headers, Class<T> responseType) {
try {
if (headers == null) {
headers = new HttpHeaders();
}
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> entity = new HttpEntity<>(requestBody, headers);
log.info("发送POST请求到: {}", url);
ResponseEntity<T> response = restTemplate.postForEntity(url, entity, responseType);
log.info("POST请求响应状态: {}", response.getStatusCode());
return response;
} catch (Exception e) {
log.error("POST请求异常: {}", e.getMessage(), e);
throw e;
}
}
// ----------------------------------PUT------------------------------------------------------- // ----------------------------------PUT-------------------------------------------------------
/** /**