Jackson Filter and Deserialize from a List of Objects

Viewed 92

Consider I have a DTO class like below:

    class MyDTO {
        Type property1;
        Type property2;
        // ... and so on
    }

Now I have an API that returns a list of MyDTO objects. But I want to filter out and get the single object MyDTO from this list of objects based on some criteria (say property1 == some value).

I know , I can do this by using streams after getting the list of objects. But is there any Jackson way of doing it? so that in my Feign Client call I can have single object as a return type.

    // returns a single object after filtering 
    // for one object while serializing.
    MyDTO feignCallApi() 
1 Answers

No. Jackson is only used for serializing/deserializing data. In your scenario described, the below are 2 possibilities:

Option 1 - API provides filtering capabilities with e.g. query params

The API you communicate with provides a possibility to filter out the desired object using some query param.

Option 2 - introduce intermediary layer in your application

Introduce an intermediary layer, e.g. a service class that utilizes the client, and performs any necessary filtering on the result of the call using e.g. a stream.

Calling class -> service class -> fiegn client

Then the service class can still have a method that returns only 1 MyDTO object.

Related