简体   繁体   English

Jackson定制过滤器,具有完整的POJO数据绑定

[英]Jackson custom filter with full POJO data bind

This question extends this question . 这个问题扩展了这个问题

While the previous solution works great if you only have a couple of fields, it becomes unmaintainable when you have more than a dozen of fields. 如果您只有几个字段,之前的解决方案效果很好,那么当您拥有十几个字段时,它就变得无法维护。 Right now, my current set up uses full data binding, so I have a POJO that will be used by Jackson to automatically deserialize JSON. 现在,我当前的设置使用完整的数据绑定,所以我有一个POJO,Jackson将使用它来自动反序列化JSON。

However, as before, certain fields have constraints that need to pass. 但是,和以前一样,某些字段具有需要通过的约束。 Essentially, I am looking for an answer similar to this , but without the need to set any properties. 基本上,我正在寻找类似于的答案,但无需设置任何属性。 Just a custom deserializer that will act as a filter and throw a custom exception if a field does not meet the constraint. 只是一个自定义反序列化器,它将充当过滤器,并在字段不符合约束时抛出自定义异常。 If no exception has been thrown by the end of the filter, Jackson should automatically bind JSON to POJO. 如果过滤器结束时没有抛出任何异常,Jackson应自动将JSON绑定到POJO。

Seems like Json Schema might fit your needs. 似乎Json Schema可能符合您的需求。 It allows for flexible (and complex) validation rules of json strings before they are deserialized. 它允许json字符串的灵活(和复杂)验证规则在反序列化之前。 It includes mandatory fields, regex-based value check, industry-standard formats (for instance, you can define a field as "email" format), cross-field dependencies (in latest v4), etc 它包括必填字段,基于正则表达式的值检查,行业标准格式(例如,您可以将字段定义为“电子邮件”格式),跨字段依赖项(在最新的v4中)等

The above is language-independant standard. 以上是与语言无关的标准。 As for Java implemenation, I used this one which supports latest json schema version (the standard is still evolving). 至于Java实现,我使用了这个支持最新的json模式版本(该标准仍在不断发展)。 The initial integration of the validator was a lot of work, (becasue of my very-dynamic json model) but after that it is very convinient to introduce new validation rules (just need to change json schema file) 验证器的初始集成是很多工作,(因为我非常动态的json模型)但是之后引入新的验证规则非常方便(只需要更改json模式文件)

I would recommend to separate concerns for deserialization and validation by using Jackson and Hibernate Vaildator correspondingly. 我建议相应地使用Jackson和Hibernate Vaildator来分离反序列化和验证的问题。 The idea is first to deserialize json data into POJO, and then validate the POJO according to the requirement. 我们的想法是首先将json数据反序列化为POJO,然后根据需求验证POJO。 In you case, you can apply Class level constraints for validation. 在这种情况下,您可以应用类级别约束进行验证。 Class level constraints have a lot of flexibility and can validate multiple correlated properties by accessing the object instance. 类级别约束具有很大的灵活性,可以通过访问对象实例来验证多个相关属性。 It is simple yet powerful. 它简单而有力。

Usually validation need more high level concerns. 通常验证需要更高层次的关注。 It is better to handle this after desrialization. 在反序列化之后处理这个问题会更好。 Doing this can make the code more easily to manage and reuse the POJO and validation rules. 这样做可以使代码更容易管理和重用POJO和验证规则。

just to consider: if you don't care about validation during deserialization, try the @JsonIgnoreProperties(ignoreUnknown = true) annotation for you POJO class. 只是考虑一下:如果你不关心反序列化过程中的验证,请为你的POJO类尝试@JsonIgnoreProperties(ignoreUnknown = true)注释。 You can do the validation later where actual business logic works with pojo classes. 您可以稍后在实际业务逻辑与pojo类一起使用时进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Jackson定制POJO的序列化 - Custom serialization of a POJO using Jackson Jackson:将 XML 中的自定义属性反序列化为 POJO - Jackson: deserialize custom attributes in XML to POJO 使用Jackson XML jar将XML数据转换为POJO? - Convert XML data to POJO with Jackson XML jar? 无法使用杰克逊,com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException将xml绑定到pojo:无法识别的字段 - can not bind xml to pojo using jackson, com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field 如何使用杰克逊ObjectMapper将POJO映射到自定义Map? - How to map a POJO to a custom Map using jackson ObjectMapper? 杰克逊定制解串器在阅读列表时创建空的pojo - Jackson custom deserializers creating empty pojo when reading list 杰克逊:反序列化地图 <String,Object> 自定义嵌套Pojo - Jackson: Deserialize map<String,Object> to Custom nested Pojo Jackson:如何在不修改 POJO 的情况下将自定义属性添加到 JSON - Jackson: How to add custom property to the JSON without modifying the POJO 如何使用 Jackson 将此动态键 JSON 反序列化为 Java 自定义 Pojo - How to deserialize this dynamic keys JSON to Java custom Pojo using Jackson 春天,最equest数据(发布)未绑定到POJO - Requst data(post) not bind to a POJO in spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM