How to Ignore unknown properties on java nested object properties

Viewed 2387

I have a parent DTO in which we have many nested objects. Is there a way i can ignore unknown properties on all the nested object as well as on the parent DTO.

If i add JsonIgnore on parent DTO, it ignores on parent class but not on nested classes. In order to make it work i have to add JsonIgnore on all the nested objects too.

Is there a way i could achieve this, without having it to write on all DTOs ?

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class RegistrationRequest implements Cloneable {
private Subject subject; 
private CaseDetail caseDetail;
private CaseEvent caseEvent; 
private List<CaseRace> caseRaces; 

private List<SubjectReference> subjectReferences; 

I have to consume a endpoint and pass this as a payload to that endpoint so thats where it is failing.

        ObjectMapper mapper = new ObjectMapper();  //TODO inject through constructor
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        Properties properties = propertiesFactory.getProperties(); //TODO inject through constructor
         url = properties.getProperty("regCore.patientInfo");   
        restclient.addHeader("userId", registrationRequest.getDataEntryPersonCtepId());
        String registrationRequestInJsonString = mapper.writeValueAsString(registrationRequest);
        response = restclient.put(url, registrationRequestInJsonString);

RestClient put request is our custom class :

public Response put(String url, String payload){
        Builder acceptInvocationBuilder = createBuilder(url);
        acceptInvocationBuilder.accept(MediaType.APPLICATION_JSON);
        return acceptInvocationBuilder.put(Entity.json(payload));
    }

The endpoint which am consuming look like below :

  @Path("/patient-demography")
    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Update patient demography", response = RegistrationRequest.class, tags = "Registration")
    public Response updatePatientDemography(Registration registration) {

It is not able to unmarshall, as it is complaining about properties doesnt match

Failed : HTTP error code : 400 Unrecognized field 
1 Answers

DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES feature comes from comes from codehaus version of Jackson. You should use fasterxml version of that feature: DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES.

Example:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        String json = "{\"id\": 11, \"nested\" : { \"x\": 1, \"y\": 2, \"z\": 3}}";

        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        Root root = mapper.readValue(json, Root.class);
        System.out.println(root);
    }
}

class Root {
    private Nested nested;

    public Nested getNested() {
        return nested;
    }

    public void setNested(Nested nested) {
        this.nested = nested;
    }

    @Override
    public String toString() {
        return "Root{" +
                "nested=" + nested +
                '}';
    }
}

class Nested {
    private int x;
    private int y;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public String toString() {
        return "Nested{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}

Prints:

Root{nested=Nested{x=1, y=2}}

Version of Jackson: 2.9.9

Related