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 Client存根生成器执行此操作可能是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验证(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.
- http://code.google.com/p/krasa-jaxb-tools/
这篇关于JAXB是否支持xsd:restriction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!