我正在尝试使用返回如下正文响应的给定 API:
"access_token": "xkeo94s4qviHSTDIuTCbgRQSeNfrrMamiCN0w6wu",
"token_type": "Bearer",
"expires_in": 9600,
"refresh_token": "PpF0LfLPmdsm9FJFu4YmDBPENqTwGQIqQjw8MqOP"
所以我创建了以下类:
@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
我可以使用以下代码发出请求并获得预期的响应正文:
@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);
}
但是当我尝试检索对 EHTLToken.class 的响应并获取它的属性时,该类被实例化,但它的所有属性都是空的。这是我正在尝试的:
@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.");
}
我的第二次测试失败:
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)
我误解了 bodyToMono() 的概念吗?请问如何从响应正文中获取属性并使用 WebClient 设置为给定的类?
问候。