RestTemplate: exchange() vs postForEntity() vs execute()

前端 未结 4 1868
说谎
说谎 2021-02-02 12:36

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,

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-02 13:01

    Execute(..) The most raw form of method, to make REST call.

    Exchange(..) A wrapper over Execute method.

    PostForEntity(..) A wrapper method, which further eases the use for making REST calls. You specify the request type in the method name itself(getForEntity, postForEntity), so, need not mention request type in the parameter. The method name in itself becomes self explanatory.

    In Exchange & postForEntity, response has to be in Json formats. This Json is further converted to Model class by json-mapper libraries. While, in Execute, we accept response in any format, as we pass the deserializer in Response Executor argument.

提交回复
热议问题