@Component
public class HttpUtil {
private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);
@Resource
private RestTemplate restTemplate;
private static HttpUtil httpUtil;
@PostConstruct
public void init(){
httpUtil = this;
httpUtil.restTemplate = this.restTemplate;
}
public static <T> String httpRequest(String url, HttpMethod method, HttpEntity<T> entity){
try {
//发起一个POST请求
ResponseEntity<String> result = httpUtil.restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
return result.getBody();
} catch (Exception e) {
logger.error("请求失败: " + e.getMessage());
}
return null;
}
}
RestTemplate使用实战-exchange方法讲解-HTTP请求
最新推荐文章于 2023-07-09 00:59:19 发布