spring boot 中访问 REST 接口

Posted 程序员之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 中访问 REST 接口相关的知识,希望对你有一定的参考价值。

RestTemplate restTemplate = new RestTemplate();
Object result = restTemplate.getForObject("https://www.baidu.com", String.class);

@Controller
 public class RestTemplateAction {
  
      @Autowired
      private RestTemplate template;
 
     @RequestMapping("RestTem")
     public @ResponseBody User RestTem(String method) {
         User user = null;
         //查找
         if ("get".equals(method)) {
             user = template.getForObject(
                     "http://localhost:8080/tao-manager-web/get/{id}",
                     User.class, "呜呜呜呜");
             
             //getForEntity与getForObject的区别是可以获取返回值和状态、头等信息
             ResponseEntity<User> re = template.
                     getForEntity("http://localhost:8080/tao-manager-web/get/{id}",
                     User.class, "呜呜呜呜");
             System.out.println(re.getStatusCode());
             System.out.println(re.getBody().getUsername());
             
         //新增
         } else if ("post".equals(method)) {
             HttpHeaders headers = new HttpHeaders();
             headers.add("X-Auth-Token", UUID.randomUUID().toString());
             MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
             postParameters.add("id", "啊啊啊");
             postParameters.add("name", "部版本");
             HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
                     postParameters, headers);
             user = template.postForObject(
                     "http://localhost:8080/tao-manager-web/post/aaa", requestEntity,
                     User.class);
         //删除
         } else if ("delete".equals(method)) {
             template.delete("http://localhost:8080/tao-manager-web/delete/{id}","aaa");
         //修改
         } else if ("put".equals(method)) {
             template.put("http://localhost:8080/tao-manager-web/put/{id}",null,"bbb");
         }
         return user;
 
     }
 }

 

RestTemplate restTemplate = new RestTemplate(); Object result = restTemplate.getForObject("https://www.baidu.com", String.class);
 
@Controller 2publicclass RestTemplateAction { 3 4 @Autowired 5private RestTemplate template; 6 7 @RequestMapping("RestTem") 8public @ResponseBody User RestTem(String method) { 9 User user = null; 10//查找11if ("get".equals(method)) { 12 user = template.getForObject( 13 "http://localhost:8080/tao-manager-web/get/{id}", 14 User.class, "呜呜呜呜"); 1516//getForEntity与getForObject的区别是可以获取返回值和状态、头等信息17 ResponseEntity<User> re = template. 18 getForEntity("http://localhost:8080/tao-manager-web/get/{id}", 19 User.class, "呜呜呜呜"); 20 System.out.println(re.getStatusCode()); 21 System.out.println(re.getBody().getUsername()); 2223//新增24 } elseif ("post".equals(method)) { 25 HttpHeaders headers = new HttpHeaders(); 26 headers.add("X-Auth-Token", UUID.randomUUID().toString()); 27 MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>(); 28 postParameters.add("id", "啊啊啊"); 29 postParameters.add("name", "部版本"); 30 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>( 31 postParameters, headers); 32 user = template.postForObject( 33 "http://localhost:8080/tao-manager-web/post/aaa", requestEntity, 34 User.class); 35//删除36 } elseif ("delete".equals(method)) { 37 template.delete("http://localhost:8080/tao-manager-web/delete/{id}","aaa"); 38//修改39 } elseif ("put".equals(method)) { 40 template.put("http://localhost:8080/tao-manager-web/put/{id}",null,"bbb"); 41 } 42return user; 4344 } 45 }

以上是关于spring boot 中访问 REST 接口的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot - REST 方法返回接口

spring boot开发REST接口

使用spring-boot对rest服务进行访问控制

docker 镜像正在运行,但无法在 Spring Boot 应用程序中访问 REST 端点

在 Spring-boot 中成功登录后如何限制 POST Rest Api 以供公共访问

Spring Boot实现RESTful接口架构实战(包括REST的讲解定义REST服务测试)