Search code examples
springresttemplate

RestTemplate only get data I care about


A RestTemplate request I am making gives a JSON request of a large data type. For example it responds with a channel object, a user object, and a preferences object.

I only need the information that relates to a channel object. Is there any way to have the responseType only deal with the channel object even though the response contains more than that?

I am only asking because I feel like it is pointless to create DTOs that contain fields that I don't care about and will never use.


Solution

  • You can create a pojo with channel object and mark pojo to ignore unknown properties ,

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Myclass {
        Channel channel;
        ...
    }
    

    In rest template ,

       restTemplate.exchange(url, HttpMethod.POST, entity, Myclass .class);