ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

RestTemplate

2019-08-03 17:03:54  阅读:199  来源: 互联网

标签:name url age RestTemplate user hobby ResponseVO


项目地址:https://gitee.com/firewolf/open-utils/tree/master/RestTemplate

一、简单介绍

RestTemplate是Spring提供的一个类似于HTTPClient的用于模拟http请求模板工具类,在Spring项目中,如果我们需要发起Http请求,可以很方便的使用这个工具类进行处理

二、GET请求

GET请求有两类:getForObject和getForEntity,其中getForObject返回的是响应体,而getForEntity返回的包括:响应行、响应头、响应体,所以这里只讲解getForObject,而忽略到getForObject

  1. public <T> T getForObject(URI url, Class<T> responseType) : 发起get请求,一般用于没有请求参数的时候。
    如果有参数的时候,需要拼接在url中,如:http://localhost:8080/user?name=zhangsan&age=123
  • url:请求的路径
  • responseType:希望返回的数据类型
  1. public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables):使用Map传递参数
    使用示例:
    Map<String, Object> args = new HashMap<>();
    args.put("name", name);
    args.put("age", age);
    args.put("hobby", StringUtils.join(hobby, ","));
    restTemplate.getForObject(USER_URL_GET + "/2?name={name}&age={age}&hobby={hobby}", ResponseVO.class, args);
  1. public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables): 和上面的方法类似,不同的是需要把所有的参数跟在后面的可变参数中,没有参数的时候也可以。
    使用示例:
restTemplate
        .getForObject(USER_URL_GET + "/2?name={name}&age={age}&hobby={hobby}", ResponseVO.class, name, age,
            StringUtils.join(hobby, ","));

三、POST请求

和GET请求一样,POST请求也有postForObject已经postForEntity两种,区别也在于前者只返回响应体,后者会返回响应头、响应行、响应体

  1. public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables):发起POST请求,各个参数含义如下:
  • url:url地址
  • request:请求实体对象
  • responseType:响应类型
  • uriVariables:url地址参数,如果url地址上没有参数的,这个参数可以不填,使用和 getForObject(String url, Class responseType, Map<String, ?> uriVariables)相同。
    需要注意的是,在传递对象的时候,如果我们使用request来传递参数,那么在接受方需要使用@RequestBody来接受参数;
    如:
    ApiUser u = new ApiUser();
    u.setHobby(hobby);
    u.setAge(age);
    u.setName(name);
    return restTemplate.postForObject(USER_URL_POST + "/1", u, ResponseVO.class);

接受方为:

  @PostMapping("/1")
  public ResponseVO addUser(@RequestBody User user) {
    log.info("user-post 1 args: {}", user);
    return ResponseVO.ok();
  }

如果使用url+uriVariables来传递参数,那么在接受方不需要使用注解来接受参数,而且,这种方式传递数组需要转成,拼接的字符串;
如:

    Map<String, Object> map = new HashMap<>();
    map.put("name", name);
    map.put("age", age);
    map.put("hobby", StringUtils.join(hobby,","));
    return restTemplate
        .postForObject(USER_URL_POST + "/2?name={name}&age={age}&hobby={hobby}", null, ResponseVO.class, map);

接受方为:

  @PostMapping("/2")
  public ResponseVO addUser2(User user) {
    log.info("user-post 2 args: {}", user);
    return ResponseVO.ok();
  }

2.public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables):这个方法和上面的类似,区别在于可以不传递参数。

  1. 模拟表单POST提交
    //设置请求数据的格式
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    //封装参数
    MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
    params.add("name", name);
    params.add("age", age);
    hobby.forEach(x->params.add("hobby",x)); //添加多值,或者是使用,分割的字符串传递数组参数

    //封装请求内容
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
    return restTemplate
        .postForObject(USER_URL_POST + "/2", requestEntity, ResponseVO.class);

接受方:

    @PostMapping("/2")
    public ResponseVO addUser2(User user) {
      log.info("user-post 2 args: {}", user);
      return ResponseVO.ok();
    }

四、PUT请求

put请求没有返回参数,有三个方法

  1. public void put(String url, @Nullable Object request, Object... uriVariables)
  • url:url地址
  • request:请求实体对象
  • uriVariables:url地址参数,如果url地址上没有参数的,这个参数可以不填,的使用和 public T getForObject(String url,Class responseType,Object… uriVariables)相同。
    和POST请求一样,有两种用法:
    a.使用request传递参数,那么后台使用@RequestBody接受参数
    ApiUser u = new ApiUser();
    u.setHobby(hobby);
    u.setAge(age);
    u.setName(name);
    restTemplate.put(USER_URL_PUT + "/1", u);
    return ResponseVO.ok();

接受方:

  @PutMapping("/1")
  public void updateUser(@RequestBody User user) {
    log.info("user-post 1 args: {}", user);
  }

b.使用url+uriVariables传递参数,那么后台直接使用对象接受参数

  @PostMapping("/2")
  public ResponseVO putWithURLArgs(String name, Integer age, @RequestParam ArrayList<String> hobby) {
    //这种方式传递数组需要用,拼接成字符串
    restTemplate.put(USER_URL_PUT + "/2?name={name}&age={age}&hobby={hobby}", null, name, age, StringUtils.join(hobby,","));
    return ResponseVO.ok();
  }

接受方:

  @PutMapping("/2")
  public void updateUser2(User user) {
    log.info("user-post 2 args: {}", user);
  }

五、DELETE请求

  1. public void delete(String url, Map<String, ?> uriVariables)
  2. public void delete(String url, Object... uriVariables)
    用法类似,就不再过多讲解了,通过url预留参数,再通过uriVariables来绑定参数

六、exchange方法

使用exchange方法可以方便的发起各种请求,方法比较多,但是大同小异,这里以其中一个方法举例说明用法。
exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables)

  • url:url地址
  • method:http请求方法,通过HttpMethod枚举类获取
  • requestEntity:请求实体,可以包含请求头和请求体的信息
  • responseType:响应类型
  • uriVariables:url地址参数,如果url地址上没有参数的,这个参数可以不填
    如利用exchange方法发起post请求
      @PostMapping("/1")
      public ResponseVO postWithPojoArgs(String name, Integer age, @RequestParam ArrayList<String> hobby) {
        ApiUser u = new ApiUser();
        u.setHobby(hobby);
        u.setAge(age);
        u.setName(name);
        HttpEntity<ApiUser> httpEntity = new HttpEntity<>(u);
        ResponseEntity<ResponseVO> exchange = restTemplate
            .exchange(USER_URL_EXCHANGE + "/1", HttpMethod.POST, httpEntity, ResponseVO.class);
        return exchange.getBody();
      }
    

接收方:
@PostMapping("/1") public ResponseVO addUser(@RequestBody User user) { log.info("user-post 1 args: {}", user); return ResponseVO.ok(); }

标签:name,url,age,RestTemplate,user,hobby,ResponseVO
来源: https://blog.csdn.net/mail_liuxing/article/details/98348369

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有