JAXB 是否支持 xsd:restriction? [英] Does JAXB support xsd:restriction?
问题描述
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
所以我希望它像这样被转换成 Java 代码:
So I want it to get converted to Java code like this:
public void setAge(int age){
if(age < 0 || age > 120){
//throw some exception
}
//setting the age as it is a valid value
}
在 JAXB 中可以吗?
Is it possible in JAXB?
曾见过一些 WebService 客户端存根生成器这样做,可能是axis2 webservice,但不确定.
Had seen some WebService Client stub generator doing this maybe axis2 webservice but not sure.
推荐答案
JAXB (JSR-222) 规范不包括在域模型中生成快速失败的逻辑.现在的一种常见做法是以注释(或 XML)的形式表达验证规则并对其运行验证.Bean 验证 (JSR-303) 对此进行了标准化并且适用于任何 Java EE 6 实现.
The JAXB (JSR-222) specification does not cover generating fail fast logic into the domain model. A common practice now is to express validation rules in the form of annotations (or XML) and run validation on them. Bean Validation (JSR-303) standardizes this and is available in any Java EE 6 implementation.
XJC 扩展
我自己没有尝试过以下扩展,但它似乎会生成 Bean Validation (JSR-303) 注释到来自 XML 模式的域模型表示验证规则.由于 XJC 具有很强的可扩展性,因此可能还有其他插件可用.
I have not tried the following extension myself but it appears as though it will generate Bean Validation (JSR-303) annotations onto the domain model representation validation rules from the XML schema. As XJC is very extensible there may be other plug-ins available as well.
这篇关于JAXB 是否支持 xsd:restriction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!