简体   繁体   English

Java Spring WebClient如何从正文响应中获取属性并设置为给定的class?

[英]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.

相关问题 如何访问 spring WebClient Retry 中的响应正文? - How to access response body in spring WebClient Retry? Spring webclient如何多次提取响应体 - Spring webclient how to extract response body multiple times 我们如何在 Spring 引导过滤器中获取和设置响应主体 - How can we get and set response body in Spring Boot Filter 在WebFlux WebClient中测试状态代码时如何获取响应体? - How to get response body when testing the status code in WebFlux WebClient? 如何从响应式 WebClient 响应中获取数据 - How to get data from reactive WebClient response 如何从具有多个属性的类中获取一个特定属性 - How to get one particular atribute from a class with several atributes 如何从 Spring Boot 过滤器中的 servletResponse 获取响应正文 - How to get Response Body from servletResponse in Spring Boot Filter 如何在将响应返回给调用者时注销对 Spring WebFlux WebClient 请求的失败响应的正文? - How do I log out the body of a failed response to a Spring WebFlux WebClient request while returning the response to the caller? Java Spring Boot 框架 - WebClient 记录 4xx/5xx 主体响应 - Java Spring Boot framework - WebClient logging 4xx/5xx body response WebClient - 如何获取请求正文? - WebClient - how to get request body?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM