如何忽略Java嵌套对象属性上的未知属性

时间:2019-10-23 21:44:28

标签: java jackson

我有一个父级DTO,其中有许多嵌套对象。 有没有一种方法可以忽略所有嵌套对象以及父DTO上的未知属性。

如果我在父DTO上添加JsonIgnore,它将在父类上忽略,但在嵌套类上不忽略。 为了使其正常工作,我还必须在所有嵌套对象上添加JsonIgnore。

有没有一种方法可以实现这一目标,而不必在所有DTO上编写它?

@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; 

我必须消耗一个端点,并将其作为有效负载传递给该端点,以便它出现故障的地方。

        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放置请求是我们的自定义类:

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

正在消耗的端点如下:

  @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) {

它无法解组,因为它抱怨属性不匹配

Failed : HTTP error code : 400 Unrecognized field 

1 个答案:

答案 0 :(得分:0)

DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES功能来自codehaus的{​​{1}}版本。您应使用该功能的Jackson版本:DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES

示例:

fasterxml

打印:

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 +
                '}';
    }
}

杰克逊的版本:2.9.9