Java Spring WebClient如何从响应体中获取属性并设置给定的类?

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

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:

&quot;access_token&quot;: &quot;xkeo94s4qviHSTDIuTCbgRQSeNfrrMamiCN0w6wu&quot;,
&quot;token_type&quot;: &quot;Bearer&quot;,
&quot;expires_in&quot;: 9600,
&quot;refresh_token&quot;: &quot;PpF0LfLPmdsm9FJFu4YmDBPENqTwGQIqQjw8MqOP&quot;

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 = &quot;/oauth/access_token&quot;;
		EHTLClient client = new EHTLClient();
		Credenciais credenciais = new Credenciais();		

		RequestHeadersSpec&lt;?&gt; 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 = &quot;/oauth/access_token&quot;;
		EHTLClient client = new EHTLClient();
		Credenciais credenciais = new Credenciais();		

		RequestHeadersSpec&lt;?&gt; request = client.getWebClient().method(HttpMethod.POST).uri(uri).bodyValue(credenciais);
		EHTLToken response = request.retrieve().bodyToMono(EHTLToken.class).block();
		
		Assert.notNull(response, &quot;Class is null.&quot;);
		Assert.notNull(response.getAccessToken(), &quot;Token is null.&quot;);
	}

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:

&quot;access_token&quot;: &quot;xkeo94s4qviHSTDIuTCbgRQSeNfrrMamiCN0w6wu&quot;,
&quot;token_type&quot;: &quot;Bearer&quot;,
&quot;expires_in&quot;: 9600,
&quot;refresh_token&quot;: &quot;PpF0LfLPmdsm9FJFu4YmDBPENqTwGQIqQjw8MqOP&quot;

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(&quot;access_token&quot;)
	private String accessToken;
	
	@JsonProperty(&quot;token_type&quot;)
	private String tokenType;
	
	@JsonProperty(&quot;expires_in&quot;)
	private String expiresIn;
	
	@JsonProperty(&quot;refresh_token&quot;)
	private String refreshToken;

I renamed the attributes as convention, and manually added the json property with the @JsonProperty.

huangapple
  • 本文由 发表于 2020年7月24日 14:21:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63067964.html
匿名

发表评论

匿名网友

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

确定