Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to receive data out of api call with webclient

So I’m trying to get my head around the webclient, but I keep getting a nullpointerexception, though my test work fine and say that object is not null. I also see my console making connection to the api. But when I ask the value, I get null.

Here are the two objects I use for it:

import com.fasterxml.jackson.annotation.JsonProperty;

public class Data {
    @JsonProperty("message")
    private String message;
    @JsonProperty("status")
    private String status;
    
    
    public String getMessage() {
        return message;
    }

    public String getStatus() {
        return status;
    }

    
}

public class Foto {
    private Data data;

    public Data getData() {
        return data;
    }

    
}

and then here I have my api call, I’ve put in my psvm to get console results:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        try {
        var test =  client.get()
                .uri("https://dog.ceo/api/breeds/image/random")
                .retrieve()
                .bodyToMono(Foto.class)
                .block();
        System.out.println(test.getData().getMessage());
    } catch (WebClientResponseException.NotFound ex) {
        ex.getMessage();
    }
        
        
    }

But as a console output for the system.out.println I keep receiving: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "be.hi10.apiesTest.domain.Data.getMessage()" because the return value of "be.hi10.apiesTest.domain.Foto.getData()" is null
at be.hi10.apiesTest.ApiesTestApplication.main(ApiesTestApplication.java:31)

What am I doing wrong here? I should be receiving a String representing a url to an image.

>Solution :

Notice that your api returns response like this :

{
"message": "https://images.dog.ceo/breeds/collie-border/n02106166_346.jpg",
"status": "success"
}

Since you are trying to convert your response to Foto class which looks like this :

public class Foto {
    private Data data;

    public Data getData() {
        return data;
    }

    
}

For that to work your JSON should have to look like this :

"data":{
     "message": "https://images.dog.ceo/breeds/collie-border/n02106166_346.jpg",
    "status": "success"
}

but it doesnt and thats why your deserilization fails and you are getting NullPointerException.

To make this work, change response type to Data :

var test =  client.get()
                .uri("https://dog.ceo/api/breeds/image/random")
                .retrieve()
                .bodyToMono(Data.class)
                .block();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading