Java Spring WebClient how to get atribute from body response and set to a given class?

1.1k Views Asked by At

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

There are 1 best solutions below

0
On BEST ANSWER

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.