当前位置:   article > 正文

spring-boot中使用RestTemplate发送post请求_springboot发送post请求

springboot发送post请求

启动类中注入容器

 在有@service注解类中,自动装载

  1. String url = "http://localhost:9083/api/update/"
  2. public String updataPerson(PersonModel personModel) {
  3. ObjectMapper objectMapper = new ObjectMapper();
  4. //设置头部
  5. HttpHeaders headers = new HttpHeaders();
  6. headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  7. String personString = null;
  8. try {
  9. personString = objectMapper.writeValueAsString(personModel);
  10. } catch (JsonProcessingException e) {
  11. throw new RuntimeException(e);
  12. }
  13. HttpEntity<String> requestEntity = new HttpEntity<>(personString,headers);
  14. // ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,personString, String.class);
  15. ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,requestEntity, String.class);
  16. System.out.println(responseEntity.getBody());
  17. return responseEntity.getBody();
  18. }

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

闽ICP备14008679号