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;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM