wangjiedadada  

使用Resttemplate可以向其他的服务接口发送请求,主要的请求方式包括get,post,put,delete,这几个主要的请求方法有包含三种不同的重载方法

这里以get为例子,分别有getForEntity(),getForObject(),getForLocation这几种构造方法;

Entity():获取全部的响应体内容;

Object():获取响应体的body内容;

Location:获取响应体的请求地址;

 

下面实例代码是getForEntity()发送请求的内容,其他getForObject(),getForLaction()跟下面实例代码类似;

以此类推,其他post,put,delete也和get方式类型,不再重复介绍。

  1 /**
  2  * ========================================================GET==========================================================
  3  */
  4 
  5 
  6 
  7     /**
  8      * restTemplate.getForEntity(url,Strig.class);
  9      * url:所要请求的服务器地址url;
 10      * String.class:请求接口后期望返回的数据类型。
 11      *
 12      * @return
 13      */
 14     @RequestMapping("/gethello")
 15     public String getHello() {
 16         ResponseEntity<String> responseEntity = restTemplate.getForEntity ("http://HELLO-SERVICE/hello", String.class);
 17         //获取响应体内容
 18         String body = responseEntity.getBody ( );
 19         //响应状态太码
 20         HttpStatus statusCode = responseEntity.getStatusCode ( );
 21         //响应状态码值
 22         int statusCodeValue = responseEntity.getStatusCodeValue ( );
 23         //响应头
 24         HttpHeaders headers = responseEntity.getHeaders ( );
 25 
 26         StringBuffer result = new StringBuffer ( );
 27         result.append ("responseEntity.getBody():").append (body).append ("<hr>")
 28                 .append ("responseEntity.getStatusCode():").append (statusCode).append ("<hr>")
 29                 .append ("responseEntity.getStatusCodeValue():").append (statusCodeValue).append ("<hr>")
 30                 .append ("responseEntity.getHeaders():").append (headers).append ("<hr>");
 31         return result.toString ( );
 32     }
 33 
 34     /**
 35      * restTemplate.getForEntity(url,String.class,name);
 36      * url:请求接口地址;
 37      * String.class:期望接口返回的数据类型;
 38      * name:给请求url参数的赋值。
 39      */
 40     @RequestMapping("/sayhello")
 41     public String sayHello() {
 42         ResponseEntity<String> responseEntity = restTemplate.getForEntity ("http://HELLO-SERVICE/sayhello?name={1}", String.class, "张三");
 43         return responseEntity.getBody ( );
 44     }
 45 
 46     /**
 47      * restForEntity(url,String.class,map)
 48      * url:请求服务接口地址
 49      * String.class:请求服务接口后期望返回的结果;
 50      * map:给url参数赋值,这里使用的是hashmap双列集合。
 51      * @return
 52      */
 53     @RequestMapping("/sayhello2")
 54     public String sayHello2() {
 55         Map<String, String> map = new HashMap<> ( );
 56         map.put ("name", "李四");
 57         ResponseEntity<String> responseEntity = restTemplate.getForEntity ("http://HELLO-SERVICE/sayhello?name={name}", String.class, map);
 58         return responseEntity.getBody ( );
 59     }
 60 
 61 
 62     /**
 63      *restTemplate.getForEntity(uri,String.claass);
 64      * uri:包含请求参数
 65      * String.class:期望返回的数据类型;
 66      */
 67     @RequestMapping("/sayhello3")
 68     public String sayHello3() {
 69         UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://HELLO-SERVICE/sayhello?name={name}").build().expand("王五").encode();
 70         URI uri = uriComponents.toUri();
 71         ResponseEntity<String> responseEntity = restTemplate.getForEntity(uri, String.class);
 72         return responseEntity.getBody();
 73     }
 74 
 75 
 76     /**
 77      *请求服务接口返回是实体类的情况。
 78      */
 79     @RequestMapping("/book1")
 80     public Book book1() {
 81         ResponseEntity<Book> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/getbook1", Book.class);
 82         return responseEntity.getBody();
 83     }
 84 
 85 
 86     /**
 87      * =========================================================POST======================================================
 88      */
 89 
 90 
 91     /**
 92      *第一个参数:请求服务器的地址
 93      * 第二个参数:请求的参数
 94      *第三个参数:期望返回的数据类型
 95      */
 96     @RequestMapping("/book3")
 97     public Book book3() {
 98         Book book = new Book();
 99         book.setName("红楼梦");
100         ResponseEntity<Book> responseEntity = restTemplate.postForEntity("http://HELLO-SERVICE/getbook2", book, Book.class);
101         return responseEntity.getBody();
102     }

 

本篇笔记总结内容是在学习下面博客内容的总结,如果需要查看完整内容,请访问下面的链接:

题目:关于RestTemplate的使用?

链接1:https://blog.csdn.net/u012702547/article/details/77917939?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0.essearch_pc_relevant&spm=1001.2101.3001.4242

 

题目2:关于RestTemplate如何发送请求内容?

链接2:https://blog.csdn.net/jinjiniao1/article/details/100849237

posted on 2021-09-02 18:59  wangjiedadada  阅读(211)  评论(0编辑  收藏  举报