jsonjackson

Avoiding writing DTO with unnecessary fields


I am making a call to third party REST Web Service. It returns me huge JSON string with lots of fields but I only need few of them. I am using jackson ObjectMapper like this :

ObjectMapper mapper = new ObjectMapper();
myDetailDto =  mapper.readValue(inputLine, new TypeReference<MyDetailDto>(){});

Is there a way I don't have to include all fields in MyDetailDto?


Solution

  • Make your MyDetailDto to have only the fields that you are interested and in the class level, add the @annotation to ignore unknown properties.

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class MyDetailDto {  }