Java @jsonview在未知属性上失败

Java @jsonview在未知属性上失败,java,spring,jackson,objectmapper,Java,Spring,Jackson,Objectmapper,我需要使用@JsonView在反序列化时抛出异常 我的POJO: public class Contact { @JsonView( ContactViews.Person.class ) private String personName; @JsonView( ContactViews.Company.class ) private String companyName; } 我的服务: public static Contact createPerson

我需要使用@JsonView在反序列化时抛出异常

我的POJO:

public class Contact
{
    @JsonView( ContactViews.Person.class )
    private String personName;

    @JsonView( ContactViews.Company.class )
    private String companyName;
}
我的服务:

public static Contact createPerson(String json) {

    ObjectMapper mapper = new ObjectMapper().configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES , true );

    Contact person = mapper.readerWithView( ContactViews.Person.class ).forType( Contact.class ).readValue( json );

    return person;
}


public static Contact createCompany(String json) {

    ObjectMapper mapper = new ObjectMapper().configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES , true );

    Contact company = mapper.readerWithView( ContactViews.Company.class ).forType( Contact.class ).readValue( json );

    return company;
}

我需要实现的是,如果我试图创建一个人,我只需要传递“personName”。如果我通过'companyName',我需要抛出异常。如何使用@JsonView实现这一点?有其他选择吗?

我认为
@JsonView
不足以为您解决这个问题。以下是更多信息,说明原因:

但我只是查看了源代码,并通过结合
@JsonView
和自定义
BeandSerializerModifier
设法解决了这个问题。这并不漂亮,但这是最重要的部分:

public static class MyBeanDeserializerModifier extends BeanDeserializerModifier {

    @Override
    public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, 
                     BeanDescription beanDesc, BeanDeserializerBuilder builder) {
        if (beanDesc.getBeanClass() != Contact.class) {
            return builder;
        }

        List<PropertyName> properties = new ArrayList<>();
        Iterator<SettableBeanProperty> beanPropertyIterator = builder.getProperties();
        Class<?> activeView = config.getActiveView();


        while (beanPropertyIterator.hasNext()) {
            SettableBeanProperty settableBeanProperty = beanPropertyIterator.next();
            if (!settableBeanProperty.visibleInView(activeView)) {
                properties.add(settableBeanProperty.getFullName());
            }
        }

        for(PropertyName p : properties){
            builder.removeProperty(p);
        }

        return builder;
    }
}
这对我很有用,我现在无法识别属性异常:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "companyName" (class Main2$Contact), not marked as ignorable (one known property: "personName"])

您可以尝试创建两个类
CompanyContact extends Contact
PersonContact extends Contact
@varren-您的意思是为“CompanyContact”和“PersonContact”创建两个新类?是的,如果您有这样的选项,它将有助于分离logic@varren-我目前只有这种结构。我所面临的问题是,我会有很多不同的观点,比如个人、公司、用户等等。我必须以这种方式创建很多类。我想知道是否有可能避免它使用JSONVIEW非常感谢!!!你帮我省了好几个小时翻阅很多帖子。成功了!再次感谢!!
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "companyName" (class Main2$Contact), not marked as ignorable (one known property: "personName"])