spring里restTemplate向目的URL发送post请求

/**
* 向目的URL发送post请求
* @param url 目的url
* @param userDTO 发送的参数
* @return ResultVO
*/
public static User sendPostRequest(String url, UserDTO userDTO, String authorization ){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", authorization);
headers.add("FROM_IN", SecurityConstants.FROM_IN);

headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity(JSONUtil.parseObj(userDTO), headers);
ResponseEntity<JSONObject> response = restTemplate.postForEntity(url, entity, JSONObject.class);
Object data = response.getBody().get("data");

ConverterRegistry converterRegistry = ConverterRegistry.getInstance();
User userInfo = converterRegistry.convert(User.class, data);

return userInfo;
}
原文地址:https://www.cnblogs.com/hmpcly/p/10250524.html