• Spring RestTemplate 之get请求


    一,简介:Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率

    二、RestTemplate中几种常见请求方法的使用

        ●get请求:在RestTemplate中,发送一个GET请求,我们可以通过如下两种方式

             第一种:getForEntity

                getForEntity方法的返回值是一个ResponseEntity<T>,ResponseEntity<T>是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。例子:

    @Controller

    @RequestMapping("/restTest")

    public class RestTempLateTest {

    private RestTemplate restTemplate = new RestTemplate();

    @RequestMapping("/hello")

    @ResponseBody

    public String getHello() {

    // ResponseEntity<IntMonitor> res = restTemplate.getForEntity(url,

    // IntMonitor)

    ResponseEntity<String> res = restTemplate.getForEntity(

    "http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor",

    String.class);

    String body = res.getBody();

    return body;

    }

    }

            有时候我在调用服务提供者提供的接口时,可能需要传递参数,有两种不同的方式,如下        

            @RequestMapping("/hello1/{flag}")

    @ResponseBody

    public String getHello1(@PathVariable String flag){

    ResponseEntity<String> res = restTemplate.getForEntity(

    "http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{1}", 

    String.class, 

    "1");

    String body = res.getBody();

    return body;

    }

            @RequestMapping("/hello2/{flag}")

    @ResponseBody

    public String getHello2(@PathVariable String flag){

    Map<String, Object> map = new HashMap<String, Object>();

    map.put("flag", flag);

    ResponseEntity<String> res = restTemplate.getForEntity(

    "http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{flag}", 

    String.class, 

    map);

    String body = res.getBody();

    return body;

    }

        

            @RequestMapping("/hello3/{flag}")

    @ResponseBody

    public String getHello3(@PathVariable String flag) {

    UriComponents uriComponents = UriComponentsBuilder

    .fromUriString(

    "http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{flag}")

    .build().expand(flag);

    URI uri = uriComponents.toUri();

    ResponseEntity<String>  res = restTemplate.getForEntity(uri, String.class);

    String body = res.getBody();

    return body;

    }

    • 可以用一个数字做占位符,最后是一个可变长度的参数,来一一替换前面的占位符

    • 也可以前面使用name={name}这种形式,最后一个参数是一个map,map的key即为前边占位符的名字,map的value为参数值

    • 调用地址也可以是一个url,而不是一个字符串,这样可以直接调用url.

            第二种:getForObject

               getForObject函数实际上是对getForEntity函数的进一步封装,如果你只关注返回的消息体的内容,对其他信息都不关注,此时可以使用getForObject,

                举一个简单的例子,如下:

            @RequestMapping("/hello4/{flag}")

    @ResponseBody

    public String getHello4(@PathVariable String flag) {

    UriComponents uriComponents = UriComponentsBuilder

    .fromUriString(

    "http://10.145.198.143:8081/ords/data_service/monitor/IntMonitor/{flag}")

    .build().expand(flag);

    URI uri = uriComponents.toUri();

    String  res = restTemplate.getForObject(uri, String.class);

    return res;

    }

  • 相关阅读:
    Linux初识(命令, 文件, 系统管理)
    【JavaScript学习整理】DOM对象(location history screen navigator)
    Charles进行HTTPS抓包(iOS为例)
    【JavaScript学习整理】js基础
    【css学习整理】浮动,清除
    【css学习整理】css基础(样式,语法,选择器)
    【html学习整理】meta,img,表格,表单
    【LeetCode】无重复字符的最长子串
    【html学习整理】常用标签
    【LeetCode】种花问题
  • 原文地址:https://www.cnblogs.com/jnba/p/10522554.html
Copyright © 2020-2023  润新知