缺少必需元素时,如何使JAXB失败? [英] How can I make JAXB to fail when a mandatory element is missing?

查看:113
本文介绍了缺少必需元素时,如何使JAXB失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在工作中调用Web服务的Java应用程序中添加一些向前兼容性,但JAXB似乎在该主题上向后行为...

I'm trying to add some forward compatibility to a Java application calling a Web Service at work, but JAXB seems to behave backward on the subject...

该应用程序使用 wsdl2java Maven插件从WSDL生成CXF Web服务客户端。然后,它将使用生成的客户端(通过JMS上的SOAP)与Web服务进行通信。
当Web服务在其对呼叫的响应中发送未知元素时,JAXB失败,并出现意外元素错误,这是可以理解的并且符合XML。为了更向前兼容,我指定了一个自定义的 jaxb-reader-validation-event-handler 来忽略这些特定的错误,从而解决了该问题。

The application use the wsdl2java Maven plugin to generate a CXF Web Service client from a WSDL. It then uses that generated client to communicate with a Web Service (through SOAP over JMS). When the Web Service sends an unknown element in its response to a call, JAXB fails with an "unexpected element" error, which is understandable, and XML-compliant. To be more forward compatible, I specified a custom jaxb-reader-validation-event-handler to ignore those specific errors, which solved the problem.

但是在进行一些补充测试时,我发现了不符合XML的行为。

首先,JAXB不在乎元素的顺序,即使在<$ c $之内c>序列,它不符合XML,但是对于前向兼容性很好,所以为什么不这样做。

但是,它也不在乎强制性元素( minOccurs = 1 )不存在,请为其分配一个任意默认值(对于绑定到Java基本值的元素,其默认值,例如<$表示为0) c $ c> int !)。

这两个都不符合XML的并且不利于兼容性:如果您需要强制性命令,请说,价格为整数,但Web Service由于某种原因未提供价格,JAXB会在没有警告的情况下将值0分配给它,这使调试工作非常困难。

显然是beca如果JAXB没有遇到元素,就使用它,只是不调用它的setter,这意味着它将保留其默认值。

But while doing some complementary tests, I discovered non-XML-compliant behaviours.
First, JAXB doesn't care about elements' order, even inside a sequence, which is not XML-compliant, but good for forward compatibility, so why not.
However, it also doesn't care if a mandatory element (minOccurs="1") is not present, kindfully assigning it an arbitrary default value (which is, for elements bound to Java primitive values, their default values, like 0 for an int!).
This is both non-XML-compliant and not good for compatibility: if you need a mandatory, say, price as an integer, but the Web Service doesn't provide it for some reason, JAXB assigns the value 0 to it without a warning, making debugging really hard.
Apparently it's because if JAXB doesn't encounter an element, it simply doesn't call its setter, which means it will keep its default value.

缺少必需元素时如何使JAXB失败?

我们注意到,即使对于具有 minOccurs = 1 的元素,相应生成的属性的注释也不包含 = true 。我尝试在生成之后和启动应用程序之前手动添加它,但是没有成功:似乎只是被忽略了……

We noticed that, even for an element with minOccurs="1", the annotation of the corresponding generated attribute does not contain required = true. I tried to add it manually after the generation and before starting the application, but with no success: it seems it's simply ignored...

推荐答案

也许额外的JSR–303验证涵盖了您想要实现的目标。您可以使用以下插件扩展xjc的行为: https://github.com/krasa/krasa -jaxb-tools 进行注释,或将Hibernate Validator与XML验证配置结合使用,如下所述: https://docs.jboss.org/hibernate/validator/4.1/reference/zh-CN/html/validator -xmlconfiguration.html#validator-xmlconfiguration

Maybe an additional JSR–303 validation covers what you want to achieve. You may either extend xjc’s behavior with a plugin like: https://github.com/krasa/krasa-jaxb-tools to have your binding classes annotated, or use Hibernate Validator in conjunction with XML validation config as described here: https://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-xmlconfiguration.html#validator-xmlconfiguration

这篇关于缺少必需元素时,如何使JAXB失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆