【问题标题】:How to Ignore unknown properties on java nested object propertiesjava - 如何忽略Java嵌套对象属性上的未知属性
【发布时间】:2019-10-23 21:44:28
【问题描述】:

我有一个父 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 put request 是我们的自定义类:

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 

【问题讨论】:

  • 这是春季项目吗?
  • 不,是 jaxrs,球衣实现

标签: java jackson


【解决方案1】:

DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES 特征来自codehaus 版本的Jackson。您应该使用该功能的fasterxml 版本:DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES

例子:

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

打印:

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

杰克逊版本:2.9.9

【讨论】:

  • 我认为你是对的,如果你像 readValue 一样解组它。但我想将该有效负载传递到另一个休息端点,因此它失败了。 public Response put(String url, String payload){ Builder acceptInvocationBuilder = createBuilder(url); acceptInvocationBuilder.accept(MediaType.APPLICATION_JSON);返回 acceptInvocationBuilder.put(Entity.json(payload)); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-21
  • 1970-01-01
  • 2013-12-28
  • 2017-09-22
  • 1970-01-01
相关资源
最近更新 更多