[英]Java Spring WebClient how to get atribute from body response and set to a given class?
I'm trying to consume a given API that returns a body response like this:我正在尝试使用返回如下正文响应的给定 API:
"access_token": "xkeo94s4qviHSTDIuTCbgRQSeNfrrMamiCN0w6wu",
"token_type": "Bearer",
"expires_in": 9600,
"refresh_token": "PpF0LfLPmdsm9FJFu4YmDBPENqTwGQIqQjw8MqOP"
So I created the following class:所以我创建了以下 class:
@JsonIgnoreProperties(ignoreUnknown = true)
public class EHTLToken {
private String access_token;
private String token_type;
private String expires_in;
private String refresh_token;
// getters and setters
I can make a request and get the expected response body with the following code:我可以使用以下代码发出请求并获得预期的响应正文:
@Test
void getTokenTest() {
String uri = "/oauth/access_token";
EHTLClient client = new EHTLClient();
Credenciais credenciais = new Credenciais();
RequestHeadersSpec<?> request = client.getWebClient().method(HttpMethod.POST).uri(uri).bodyValue(credenciais);
String response = request.retrieve().bodyToMono(String.class).block();
System.out.println(response);
}
But when I try to retrieve the response to the EHTLToken.class and get its atributes, the class is is instantiated, but all it's atributes are null.但是当我尝试检索对 EHTLToken.class 的响应并获取其属性时, class 被实例化,但它的所有属性都是 Z37A6259CC0C1DAE299A7866489DFF0 Here's what I'm trying:
这是我正在尝试的:
@Test
void getTokenTest() {
String uri = "/oauth/access_token";
EHTLClient client = new EHTLClient();
Credenciais credenciais = new Credenciais();
RequestHeadersSpec<?> request = client.getWebClient().method(HttpMethod.POST).uri(uri).bodyValue(credenciais);
EHTLToken response = request.retrieve().bodyToMono(EHTLToken.class).block();
Assert.notNull(response, "Class is null.");
Assert.notNull(response.getAccessToken(), "Token is null.");
}
My second test fails:我的第二次测试失败:
java.lang.IllegalArgumentException: Token is null.
at org.springframework.util.Assert.notNull(Assert.java:198)
at br.com.ribeiro.fernando.ehtl.EhtlApplicationTests.getTokenTest(EhtlApplicationTests.java:27)
Am I misunderstanding the concept of bodyToMono()?我误解了 bodyToMono() 的概念吗? How can I get atributes from a response body and set to a given class with WebClient please?
如何从响应正文中获取属性并使用 WebClient 设置为给定的 class?
Regards.问候。
For anyone having this issue, my problem is that the REST API gives a response like this:对于遇到此问题的任何人,我的问题是 REST API 给出如下响应:
"access_token": "xkeo94s4qviHSTDIuTCbgRQSeNfrrMamiCN0w6wu",
"token_type": "Bearer",
"expires_in": 9600,
"refresh_token": "PpF0LfLPmdsm9FJFu4YmDBPENqTwGQIqQjw8MqOP"
And I created the POJO with the following attributes:我创建了具有以下属性的 POJO:
private String access_token;
private String token_type;
private String expires_in;
private String refresh_token;
My test worked when I changed my POJO to this:当我将 POJO 更改为此时,我的测试有效:
@JsonIgnoreProperties(ignoreUnknown = true)
public class EHTLToken {
@JsonProperty("access_token")
private String accessToken;
@JsonProperty("token_type")
private String tokenType;
@JsonProperty("expires_in")
private String expiresIn;
@JsonProperty("refresh_token")
private String refreshToken;
I renamed the attributes as convention, and manually added the json property with the @JsonProperty.我将属性重命名为约定,并使用@JsonProperty 手动添加了 json 属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.