RestTemplate:exchange() vs postForEntity() vs execute() [英] RestTemplate: exchange() vs postForEntity() vs execute()

查看:262
本文介绍了RestTemplate:exchange() vs postForEntity() vs execute()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Boot 处理 Rest API,我必须访问应用程序的端点.我使用了 RestTemplate.我能够使用两种方法来做到这一点,

I am working on Rest API using Spring boot and I had to access an application's endpoint. I used RestTemplate for it. I was able to do it using 2 methods,

  • postForEntity():

responseEntity = 
    restTemplate.postForEntity(uri, httpEntity, ResponseClass.class);

  • exchange():

    responseEntity = 
        restTemplate.exchange(uri, HttpMethod.POST, httpEntity, ResponseClass.class);
    

  • 我想知道这两种方法的用法和区别.

    I would like to know the usage and differences of these two methods.

    我还看到了另一个方法 execute().请解释一下.如何以及何时使用它.

    I also see another method execute(). Please shed some light on it. How and when to use it.

    推荐答案

    RestTemplate 是一个非常通用的对象.

    The RestTemplate is a very versatile object.

    让我们从 execute 开始,因为它是最通用的方法:

    Let's start with execute, since it's the most generic method:

    execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback,
            @Nullable ResponseExtractor<T> responseExtractor, Object... uriVariables)
    

    注意 uriVariables 也可以作为 Map 传递.

    Note the uriVariables can be passed as a Map too.

    execute 旨在适用于尽可能多的场景:

    execute is designed to be applicable in the highest variety of scenarios possible:

    • 第一个和第二个参数允许 URL 和方法的任何有效组合.
    • 通过传递自定义的RequestCallback(一个@FunctionalInterface,只有一个方法doWithRequest(ClientHttpRequest request)) 发送之前.
    • 可以通过传递自定义 ResponseExtractor 以任何必要的方式反序列化从远程资源返回的响应.
    • The first and second parameters allow any valid combination of URL and method.
    • The request can be modified in a myriad of different ways by passing a custom RequestCallback (a @FunctionalInterface with just one method doWithRequest(ClientHttpRequest request)) before sending it.
    • The response returned from the remote resource can be deserialized in any way necessary by passing a custom ResponseExtractor.

    将此与 exchange 进行比较:

    Compare this with exchange:

    exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity,
             Class<T> responseType, Object... uriVariables)
    

    这里有两个主要区别:

    • 您现在可以直接传递 HttpEntity,而在此之前需要使用 RequestCallback 手动设置.
    • 通过传递所需的响应类型 Class 来提供开箱即用的反序列化机制.
    • You can now pass an HttpEntity directly, whereas before it needed to be set manually using the RequestCallback.
    • Deserialization mechanics are provided out of the box by passing the desired response type Class.

    如您所见,这对于日常使用来说更加方便.

    As you can see, this is much more convenient for everyday use.

    getForEntitypostForEntity 这样的方法更短、更容易理解:

    Methods like getForEntity and postForEntity are even shorter, easier to understand versions of this:

    getForEntity(String url, Class<T> responseType, Object... uriVariables)
    
    postForEntity(String url, @Nullable Object request, Class<T> responseType,
                  Object... uriVariables)
    

    注意 postForEntity 现在允许您直接 POST 任何 Object 而无需包装器.使用这些代替 execute 没有性能优势或损害,因为它们在幕后调用 execute - 这只是一个方便的问题.

    Notice postForEntity now allows you to POST any Object directly without a wrapper. There is no performance benefit or detriment to using these instead of execute, as they call execute themselves under the hood - it's simply a matter of convenience.

    这篇关于RestTemplate:exchange() vs postForEntity() vs execute()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆