[英]Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?
If I understand the spirit of JSR-303 correctly, it is intended to allow the application of Bean (data) Validation at multiple layers of an application architecture. 如果我正确理解JSR-303的精神,那么它旨在允许在应用程序体系结构的多个层上应用Bean(数据)验证。
Whilst I have seen some discussion around JPA / Hibernate applications of JSR-303, I am struggling to find whether there are any working implementations that may be applied to Web Services / JAXB unmarshalling (ie. closer to the presentation-layer). 虽然我已经看到围绕JSR-303的JPA / Hibernate应用程序的一些讨论,但我很难找到是否有任何可以应用于Web Services / JAXB解组的工作实现(即更接近表示层)。
Here is a fairly contrived example of something I would like to fail due to Bean Validation: 这是一个相当人为的例子,我想因Bean验证而失败:
Square.class Square.class
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlRootElement(name = "square")
public class Square {
@Size(min = 4, max = 4)
private int numberOfSides;
public void setNumberOfSides(int numSides) {
this.numberOfSides = numSides;
}
public int getNumberOfSides() {
return this.numberOfSides;
}
}
Test class 考试班
@RunWith(JUnit4.class)
public class BeanValidationTest {
@Test
public void Should_not_unmarshal_invalid_numberOfSides_value() throws JAXBException {
String xml = "<square>" +
" <numberOfSides>3</numberOfSides>" +
"</square>";
JAXBContext context = org.eclipse.persistence.jaxb.JAXBContext.newInstance(Square.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Square result = (Square) unmarshaller.unmarshal(new StringReader(xml));
assertThat(result.getNumberOfSides(), equalTo(4));
}
}
NOTE: The test will fail due to the expected number of sides constraint, it fails with this: 注意:由于预期的边数限制,测试将失败,因此失败:
java.lang.AssertionError:
Expected: <4>
but: was <3>
I would re-write to expect the Bean Validation exception, except that I do not know what to expect because Bean Validation does not fire. 我会重写以期待Bean Validation异常,除了我不知道会发生什么,因为Bean Validation不会触发。
My confusion is that I think that I should not even reach the assertion in the test above, and that the 3 value was successfully deserialised without either: 我的困惑是,我认为我甚至不应该在上面的测试中达到断言,并且3值被成功反序列化而没有:
So, in summary, I think that leaves me a choice of questions, either: 总而言之,我认为这让我可以选择以下问题:
A. How do I instruct EclipseLink MOXy to apply Bean Validations, or should it at least warn me if it cannot find a JSR-303 implementation in the class path? A.如何指示EclipseLink MOXy应用Bean Validations,或者它是否至少警告我,如果它在类路径中找不到JSR-303实现?
B. Or, is there another (better?) way to implement what I have asked for? B.或者,还有另一种(更好的)方式来实现我的要求吗?
(assume that I know I can manually validate and/or implement an @XmlJavaTypeAdapter
if necessary - they are just not my preferred elegant solutions) (假设我知道我可以在必要时手动验证和/或实现
@XmlJavaTypeAdapter
- 它们不是我首选的优雅解决方案)
Versions (if relevant): 版本(如果相关):
The only available solution for you is using MOXy version 2.6+ as JAXB provider. 唯一可用的解决方案是使用MOXy 2.6+作为JAXB提供程序。 What you want will then be done automatically for you in default configuration (this BV support can be turned off).
在默认配置中,您将自动完成所需的操作(可以关闭此BV支持)。 MOXy is currently tied to EclipseLink project, so you would have to use EclipseLink (it also implements JPA, SDO, JSON).
MOXy目前与EclipseLink项目相关联,因此您必须使用EclipseLink(它还实现了JPA,SDO,JSON)。
At the moment, there is version 2.6.0-M3 of EclipseLink available at: sonatype or maven . 目前,有一个版本2.6.0-M3的EclipseLink可用于: sonatype或maven 。
Release is expected in Q1-2015. 预计将于2015年第一季度发布。
I think what you are asking for is coming with Eclipse Link 2.6.0. 我认为你要求的是Eclipse Link 2.6.0。 See also https://bugs.eclipse.org/bugs/show_bug.cgi?id=431803 and https://wiki.eclipse.org/EclipseLink/Development/2.6.0
另见https://bugs.eclipse.org/bugs/show_bug.cgi?id=431803和https://wiki.eclipse.org/EclipseLink/Development/2.6.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.