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;
}
posted @ 2019-01-10 16:01  闭眼的鱼  阅读(1521)  评论(0编辑  收藏  举报