首页 > 解决方案 > 嵌入式绑定不适用于 jaxb xjc 3.0.0

问题描述

我正在使用 jaxb 3.0.0 (xjc.sh) 创建 Java 类,但由于某种原因,它不支持嵌入式绑定。同样的事情适用于 jaxb 2.3.1

$密码/c/jaxb-ri-3.0.0/jaxb-ri/bin

$ ./xjc.sh 验证.xsd

它正在创建 ApiValidator.java,我希望它不会创建一个新的,而不是引用我作为嵌入式绑定放置的那个。

$ cat 验证.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="a/model"
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
           xmlns:tns="a/model" elementFormDefault="qualified">

    <xs:attribute name="Description" type="xs:string"/>
    <xs:attribute name="Required" type="xs:boolean"/>

    <xs:complexType name="ValidationAction">
        <xs:sequence>
            <xs:element name="api" type="xs:string" tns:Description="Indicates the validation API"/>
            <xs:element name="method" type="xs:string" tns:Description="Indicates the validation method"/>
            <xs:element name="beanName" type="xs:string" tns:Description="Validation bean name"/>
            <xs:element name="validationClass" type="xs:string" tns:Description="Validation class type"/>
            <xs:element name="idOwner" type="xs:int" tns:Description="Indicates the owner associated with the validation"/>
            <xs:element name="valid" type="xs:boolean" tns:Description="Indicates if validation is valid"/>
            <xs:element name="validator" type="tns:ApiValidator" minOccurs="0" maxOccurs="1"
                        tns:Description="Reference to the API validator"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="ApiValidator">
        <xs:annotation>
            <xs:appinfo>
                <jaxb:bindings node="//xs:complexType[@name='ApiValidator']">
                    <jaxb:class ref="a.ApiValidator"/>
                </jaxb:bindings>
            </xs:appinfo>
        </xs:annotation>
    </xs:complexType>

</xs:schema>

而如果我对 2.3.1 版本做同样的事情,$ ./xjc.sh Validation.xsd Java 主要版本:8 解析模式......编译模式......

我需要使用 3.0.0 版本,因为它会生成 jakarta 命名空间。请建议为什么它不起作用以及如何解决它?

这种行为与下面提到的 xml 相同:

$ cat V.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="a/model"
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
           xmlns:tns="a/model" elementFormDefault="qualified">

    <xs:attribute name="Description" type="xs:string"/>
    <xs:attribute name="Required" type="xs:boolean"/>

    <xs:complexType name="ValidationAction">
        <xs:sequence>
            <xs:element name="api" type="xs:string" tns:Description="Indicates the validation API"/>
            <xs:element name="method" type="xs:string" tns:Description="Indicates the validation method"/>
            <xs:element name="beanName" type="xs:string" tns:Description="Validation bean name"/>
            <xs:element name="validationClass" type="xs:string" tns:Description="Validation class type"/>
            <xs:element name="idOwner" type="xs:int" tns:Description="Indicates the owner associated with the validation"/>
            <xs:element name="valid" type="xs:boolean" tns:Description="Indicates if validation is valid"/>
            <xs:element name="validator" type="tns:ApiValidator" minOccurs="0" maxOccurs="1"
                        tns:Description="Reference to the API validator"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="ApiValidator">
        <xs:annotation>
            <xs:appinfo>
                <jaxb:class ref="rd.ApiValidator"/>
            </xs:appinfo>
        </xs:annotation>
    </xs:complexType>


</xs:schema>

如建议: Jaxb implClass 规范忽略了 rootElement

标签: bindingjaxbxjc

解决方案


原来,我不得不改变这一行:

  • xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0" 到
  • xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb" jaxb:version="3.0"

推荐阅读