【问题标题】:What is the restTemplate.exchange() method for?restTemplate.exchange() 方法有什么用?
【发布时间】:2013-12-09 18:59:03
【问题描述】:

restTemplate.exchange() 方法实际上是做什么的?

@RequestMapping(value = "/getphoto", method = RequestMethod.GET)
public void getPhoto(@RequestParam("id") Long id, HttpServletResponse response) {

    logger.debug("Retrieve photo with id: " + id);

    // Prepare acceptable media type
    List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
    acceptableMediaTypes.add(MediaType.IMAGE_JPEG);

    // Prepare header
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(acceptableMediaTypes);
    HttpEntity<String> entity = new HttpEntity<String>(headers);

    // Send the request as GET
    ResponseEntity<byte[]> result = 
        restTemplate.exchange("http://localhost:7070/spring-rest-provider/krams/person/{id}", 
                              HttpMethod.GET, entity, byte[].class, id);

    // Display the image
    Writer.write(response, result.getBody());
}

【问题讨论】:

    标签: rest resttemplate


    【解决方案1】:

    exchange 方法针对指定的 URI 模板执行 HTTP 方法,传入参数进行替换。在这种情况下,它会为其 Id 参数获取一个人实体的图像,并为其返回字节数组。

    【讨论】:

      【解决方案2】:

      method documentation 非常简单:

      对给定的 URI 模板执行 HTTP 方法,将给定的请求实体写入请求,并将响应返回为ResponseEntity

      使用给定的 URI 变量扩展 URI 模板变量(如果有)。


      考虑以下从您自己的问题中提取的代码:

      ResponseEntity<byte[]> result = 
          restTemplate.exchange("http://localhost:7070/spring-rest-provider/krams/person/{id}", 
                                HttpMethod.GET, entity, byte[].class, id);
      

      我们有以下内容:

      • 将向给定 URL 执行 GET 请求,发送封装在 HttpEntity 实例中的 HTTP 标头。
      • 给定的 URL 包含一个模板变量 ({id})。它将替换为最后一个方法参数 (id) 中给出的值。
      • 响应实体将作为 byte[] 包装到 ResponseEntity 实例中返回。

      【讨论】:

      • 这个方法是同步的还是异步的?根据描述,它是同步的。我想知道它如何处理从服务器返回 Future 的请求。
      • RestTemplate 是同步的。请参阅documentation
      • 您的解释很有帮助。但是OP是对的,原文本质上是循环推理。只有已经理解的人才能理解。我最终来到这里是因为我正在尝试移植一个使用 exchange() 的脚本,而该文档是毫无希望的。
      【解决方案3】:

      更通用的交换 API 需要一个 HttpMethod 参数和一个请求对象来保证完整性。比较:

      ResponseEntity<Foo> response = 
      restTemplate.exchange(url, HttpMethod.GET, request, Foo.class);
      
      ResponseEntity<Foo> response = 
      restTemplate.getForEntity(url, Foo.class);
      

      【讨论】:

        【解决方案4】:

        TL;DR: Q: What is a request-response pair called? A: 一个“exchange”。


        the official technical documentation of HTTP 中使用exchange 一词来指代与相应响应相结合的HTTP 请求。

        然而,看看以下问题的答案,很明显,虽然这对某些人来说可能代表了事实上的标准,但其他许多人并没有意识到它,或者没有采用它。

        documentation 懒得提这个名字的词源——可能是假设它很明显。

        但是请注意,列出了许多不同的 RestTemplate HTTP 请求方法,其中只有一小部分被命名为 exchange。该列表主要由HTTP method 特定名称组成,例如deleteputgetForEntitypostForObject 等。从技术上讲,所有这些方法都以相同的方式执行交换,但更集中的便捷方法仅限于可能的交换功能和参数+返回类型的特定子集。

        简单地说,exchange 函数集是RestTemplate 提供的最通用/功能最强大的方法,所以当其他方法都不提供时,你可以使用exchange完成足够的参数集以满足您的需求。

        例如:

        【讨论】:

          猜你喜欢
          • 2020-09-13
          • 2018-10-23
          • 2019-12-13
          • 1970-01-01
          • 2013-03-02
          • 1970-01-01
          • 2010-12-13
          • 2017-01-29
          • 2010-10-27
          相关资源
          最近更新 更多