当前位置:   article > 正文

RestTemplate 发送post请求_resttemplate发送post

resttemplate发送post
  1. private RestTemplate getRestTemplate() {
  2. RestTemplate restTemplate = new RestTemplate();
  3. ExtendMappingJackson2HttpMessageConverter convert
  4. = new ExtendMappingJackson2HttpMessageConverter();
  5. convert.setObjectMapper(new ObjectMapper());
  6. restTemplate.getMessageConverters().add(convert);
  7. return restTemplate;
  8. }
  9. private String post(String url, LinkedMultiValueMap<String, Object> params) {
  10. HttpHeaders headers = new HttpHeaders();
  11. headers.add("Content-Type", "application/x-www-form-urlencoded");
  12. HttpEntity<LinkedMultiValueMap<String, Object>> request = new HttpEntity<>(params, headers);
  13. ResponseEntity<String> result = getRestTemplate().postForEntity(url,
  14. request, String.class);
  15. return result.getBody();
  16. }
  17. --------------------------------------------------------------
  18. public class ExtendMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
  19. public ExtendMappingJackson2HttpMessageConverter() {
  20. List<MediaType> mediaTypes = new ArrayList<>();
  21. mediaTypes.add(MediaType.ALL);
  22. //List<MediaType> mediaTypes = getSupportedMediaTypes();
  23. //mediaTypes.add(MediaType.TEXT_HTML);
  24. setSupportedMediaTypes(mediaTypes);
  25. }
  26. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/303078
推荐阅读
相关标签
  

闽ICP备14008679号