JAXB生成了简单类型缺失限制

Chr*_*ght 3 java jaxb

我正在生成我的第一个JAXB数据绑定.我有一个架构,其中包含一个xs:simpleType:

<xs:simpleType name="NINumberType">
    <xs:restriction base="xs:string">
        <xs:pattern value="[A-Z]{2}\d{6}[A-D]{0,1}"/>
    </xs:restriction>
</xs:simpleType>
Run Code Online (Sandbox Code Playgroud)

在我的binding.xjb中我有这个:

<jxb:bindings version="1.0"
     xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
     jxb:extensionBindingPrefixes="xjc">

     <jxb:bindings schemaLocation="mySchema.xsd" node="/xs:schema">
         <jxb:globalBindings mapSimpleTypeDef="true" />
         <jxb:schemaBindings>
             <jxb:package name="com.company.jaxb.mySchema"/>
         </jxb:schemaBindings>
     </jxb:bindings>

</jxb:bindings>
Run Code Online (Sandbox Code Playgroud)

然后,通过Eclipse(RAD 7.5),我右键单击模式,然后选择Generate-> Java.这会产生预期的数据绑定对象,除了NINumberType没有内置限制:

/**
 * <p>Java class for NINumberType simple type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;simpleType name="NINumberType">
 *   &lt;restriction base="{http://www.w3.org/2001/XMLSchema}string">
 *     &lt;pattern value="[A-Z]{2}\d{6}[A-Z]{0,1}"/>
 *   &lt;/restriction>
 * &lt;/simpleType>
 * </pre>
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "NINumberType", propOrder = {"value"})
public class NINumberType {
    @XmlValue
    protected String value;

    public String getValue() {return value;}
    public void setValue(String value) {this.value = value;}
}
Run Code Online (Sandbox Code Playgroud)

除了类级别javadoc之外,没有提到我的模式中指定的正则表达式限制.JAXB似乎具有生成限制代码所需的信息,但未使用它.任何人都可以帮我确保生成限制代码,以便尝试绑定格式不佳的NI编号会失败吗?

bdo*_*han 7

JAXB不会在Java模型中生成这些限制.您可以指定实例javax.xml.validation.SchemaMarshaller/ Unmarshaller如果你想这个约束从XML转换为/时执行.

欲获得更多信息


您可能能够找到利用JSR-303之类的XJC扩展来进行验证约束.以下链接可能有所帮助: