RestTemplate中的exchange方法的目的是什么?

huangapple go评论56阅读模式
英文:

What is the purpose of the exchange method in the RestTemplate?

问题

以下是翻译好的部分:

"我目前正在向客户发送一个资源,我正在使用已经完成的代码并对其进行修改,这段代码中有一行我不太理解。我理解我正在发送或发布一个资源,我理解这个方法需要客户端的URL,我理解它需要HTTP请求的类型,例如在这种情况下是POST,但我不明白为什么这个方法需要 nService.getStringHttpEntityWithPayload(payLoad) 和 Resource.class?此外,它返回的响应实体将只是一个类,还是一个带有状态和标头的类?"

请注意,我只提供了对代码段的翻译,不回答翻译请求之外的问题。

英文:

I am currently sending a resource to a client, I am using code that has been done already and I am modifying it, there is a line shown below in this code that I don't understand. Well I understand that I am sending or posting a resource, I understand this method takes the url of the client, that it takes the type of HTTP request for example in this case POST, but I dont understant why this method takes nService.getStringHttpEntityWithPayload(payLoad) and Resource.class? Also the response entity it is returning will it be a class only or a class with a status and a headers?

ResponseEntity<Resource> responseEntity = restTemplate.exchange(
                eURL,
                HttpMethod.POST,
                nService.getStringHttpEntityWithPayload(payLoad),
                Resource.class);

答案1

得分: 0

why this method takes nService.getStringHttpEntityWithPayload(payLoad) and Resource.class?

这个方法为什么要接收 nService.getStringHttpEntityWithPayload(payLoad)Resource.class 作为参数?

The method getStringHttpEntityWithPayload is returning a HttpEntity which is composed of a body and header data to be sent to a URL. The method is creating the request message by adding the content type header, letting the receiving service know that the body contains JSON data.

方法 getStringHttpEntityWithPayload 返回一个 HttpEntity,其中包含要发送到URL的主体和头部数据。该方法通过添加内容类型头部来创建请求消息,以通知接收服务主体包含JSON数据。

The parameter Resource.class is used to determine what class to deserialize the response body from the service into. It defines the generic type of the return value: ResponseEntity<Resource>.

参数 Resource.class 用于确定从服务中反序列化响应主体的类是什么。它定义了返回值的泛型类型:ResponseEntity<Resource>

Also the response entity it is returning will it be a class only or a class with a status and a headers?

另外,返回的响应实体只是一个类还是一个带有状态和头部的类?

I'm not sure what you mean by "class only". The ResponseEntity is similar to HttpEntity (in fact class ResponseEntity<T> extends HttpEntity<T>). The ResponseEntity class contains the response body and headers, as well as the HTTP Status code of the response.

我不确定你所说的“只是一个类”是什么意思。ResponseEntity 类类似于 HttpEntity(实际上是 class ResponseEntity<T> extends HttpEntity<T>)。ResponseEntity 类包含响应主体和头部,以及响应的HTTP状态代码。

英文:

> why this method takes nService.getStringHttpEntityWithPayload(payLoad) and Resource.class?

The method getStringHttpEntityWithPayload is returning a HttpEntity which is composed of a body and header data to be sent to a URL. The method is creating the request message by adding the content type header, letting the receiving service know that the body contains JSON data.

The parameter Resource.class is used to determine what class to deserialize the response body from the service into. It defines the generic type of the return value: ResponseEntity&lt;Resource&gt;.

> Also the response entity it is returning will it be a class only or a class with a status and a headers?

I'm not sure what you mean by "class only". The ResponseEntity is similar to HttpEntity (in fact class ResponseEntity&lt;T&gt;
extends HttpEntity&lt;T&gt;
). The ResponseEntity class contains the response body and headers, as well as the HTTP Status code of the response.

huangapple
  • 本文由 发表于 2020年8月14日 12:16:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63406458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定