2

Small question regarding Spring Webflux, and how to get the nested List of Pojo that is present in a http response directly.

We are consuming an API which response is something like

{
    "noNeedThisField": "I do not need this",
    "listOfWhatIwant": [
        {
            "personName": "Alice",
            "personAge": "11"
        },
        {
            "personName": "Bob",
            "personAge": "22"
        },
        {
            "personName": "Charlie",
            "personAge": "33"
        }
    ],
    "uselessField": "This is useless", 
    "manyFieldsNoNeed": "it is one response, which contains a lot of fields that I do not need, I just need to retrieve the list DIRECTLY please",
    "noNeed": true,
    "anotherNotImportant": "this is not important at all"
}

Basically, it is one response, which contains a lot of fields I do not need, plus an element of type list in it, which I would like to get directly.

If I create two different classes, first one

public class PojoWithListAndOtherNoNeedFields {

private     String             noNeedThisField;
    private List<MyNestedPojo> listOfWhatIwant;
    private String             uselessField;
    private String             manyFieldsNoNeed;
    private boolean            noNeed;
    private String             anotherNotImportant;
}

//getters setters

second one


public class MyNestedPojo {

    private String             personName;
    private String             personAge;

//getters setters

}

And invokes Webclient like this:

public Mono<PojoWithListAndOtherNoNeedFields> sendReqest() {
        return webClient.mutate().baseUrl("url").build().post().uri("/route").retrieve().bodyToMono(PojoWithListAndOtherNoNeedFields.class);
    }

It is working fine! I just need to carry a very large class that I do not need in my code, and retrieve the nested list of what I need with a getter each time.

However, I was wondering is it is possible to do something similar as (this is not working)

public Mono<List<MyNestedPojo>> sendReqest() {
        return webClient.mutate().baseUrl("url").build().post().uri("/route").retrieve().bodyToMono(List<MyNestedPojo>.class);
    }

In order to retrieve the nested element directly. My goal is to get rid of PojoWithListAndOtherNoNeedFields entirely, and getting the List< MyNestedPojo> directly. Is it possible?

How to perform this in a proper way in Spring using the Webclient please?

Thank you

PatPanda
  • 3,644
  • 9
  • 58
  • 154

1 Answers1

2

You can use the @JsonIgnoreProperties annotation to inform the ObjectMapper to ignore any fields not included in your POJO when deserialisating from json to a POJO.

@JsonIgnoreProperties(ignoreUnknown = true)
public class PojoWithListAndOtherNoNeedFields {
    private List<MyNestedPojo> listOfWhatIwant;

}

public class MyNestedPojo {
    private String personName;
    private String personAge;
}

JavaDocs

Michael McFadyen
  • 2,675
  • 11
  • 25
  • Of course, my goal is to get rid of PojoWithListAndOtherNoNeedFields entirely, and getting the List< MyNestedPojo> directly. Is it possible? – PatPanda Feb 10 '21 at 22:39
  • 1
    only if you feel like writing a custom deserialiser, and register it with the objectmapper, which would be overkill. i'd say, deserialise the response using the answer above, then just get the list and you are done. – Toerktumlare Feb 11 '21 at 00:03
  • I am actually looking for the "overkill solution". For educational purpose and to know the technique of how to do it without having to write two classes, where the first one is just a getter to the second class that is actually needed – PatPanda Feb 11 '21 at 22:55
  • there are plenty of already answered stack overflow questions on writing a custom deserialiser for jackson. Here is one example: https://stackoverflow.com/questions/19158345/custom-json-deserialization-with-jackson – Michael McFadyen Feb 14 '21 at 19:47