英文:
Java Spring WebClient how to get atribute from body response and set to a given class?
问题
以下是翻译好的内容:
我正在尝试使用一个给定的API,该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 并获取其属性时,该类会被实例化,但所有属性均为null。以下是我的尝试:
@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 从响应体中获取属性并设置给定的类呢?
谢谢。
英文:
I'm trying to consume a given API that returns a body response like this:
"access_token": "xkeo94s4qviHSTDIuTCbgRQSeNfrrMamiCN0w6wu",
"token_type": "Bearer",
"expires_in": 9600,
"refresh_token": "PpF0LfLPmdsm9FJFu4YmDBPENqTwGQIqQjw8MqOP"
So I created the following 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. 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()? How can I get atributes from a response body and set to a given class with WebClient please?
Regards.
答案1
得分: 0
对于遇到这个问题的任何人,我的问题是REST API返回的响应如下:
"access_token": "xkeo94s4qviHSTDIuTCbgRQSeNfrrMamiCN0w6wu",
"token_type": "Bearer",
"expires_in": 9600,
"refresh_token": "PpF0LfLPmdsm9FJFu4YmDBPENqTwGQIqQjw8MqOP"
我创建了以下属性的POJO:
private String access_token;
private String token_type;
private String expires_in;
private String refresh_token;
当我将我的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;
}
我按照约定重新命名了属性,并手动使用了@JsonProperty添加了JSON属性。
英文:
For anyone having this issue, my problem is that the REST API gives a response like this:
"access_token": "xkeo94s4qviHSTDIuTCbgRQSeNfrrMamiCN0w6wu",
"token_type": "Bearer",
"expires_in": 9600,
"refresh_token": "PpF0LfLPmdsm9FJFu4YmDBPENqTwGQIqQjw8MqOP"
And I created the POJO with the following attributes:
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:
@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.
专注分享java语言的经验与见解,让所有开发者获益!



评论