JAXB2 基础插件 - 我的外部绑定自定义文件中的 schemaLocation 问题
JAXB2 Basics Plugins - Issue with schemaLocation in my External binding customization file
我正在使用 JAXB 从我的 XSD 文件生成 类。我想让生成的 类 实现一个通用接口。因此,我正在尝试使用 external binding file 方法来执行 JAXB2 Basics 插件。这是我的自定义绑定文件:
customBindingFile.xjb
<?xml version="1.0"?>
<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"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jxb:extensionBindingPrefixes="xjc">
<jxb:bindings schemaLocation="abc-api.xsd">
<jxb:bindings node="//xs:complexType[@name='MyClass']">
<inheritance:implements>com.kuldeep.CommonInterface</inheritance:implements>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
以下是我用于生成源代码的 pom 文件中的 maven 插件:
请注意 我添加的注释 是我对现有插件条目所做的更改。
pom.xml
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.plugin.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<!-- **extensions and args added by me** -->
<extensions>
<extension>org.jvnet.jaxb2_commons:jaxb2-basics:0.9.2</extension>
</extensions>
<args>
<arg>-Xinheritance</arg>
</args>
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
<defaultOptions>
<bindingFiles>
<bindingFile>src/main/resources/jaxws_binding.xml</bindingFile>
<bindingFile>src/main/resources/jaxb_binding.xml</bindingFile>
</bindingFiles>
</defaultOptions>
<wsdlOptions>
......
<wsdlOption>
<wsdl>${project.build.directory}/generated/framework/cxf/abc-api-inline.wsdl</wsdl>
<!-- **bindingFile added by me** -->
<bindingFile>src/main/resources/customBindingFile.xjb</bindingFile>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<!-- **dependency added by me** -->
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.9.2</version>
</dependency>
</dependencies>
</plugin>
我遇到的问题是我的模式文件 abc-api.xsd 驻留在其他一些项目中,所以当我尝试执行 maven 安装以生成我的 类,我收到错误提示 abc-api.xsd 不是此编译的一部分。
[ERROR] Failed to execute goal
org.apache.cxf:cxf-codegen-plugin:3.0.3:wsdl2java (generate-sources)
on project : Execution generate-sources of goal
org.apache.cxf:cxf-codegen-plugin:3.0.3:wsdl2java failed:
file:/I:/project/src/main/resources/customBindingFile.xjb [9,56]:
"file:/I:/project/src/main/resources/abc-api.xsd" is not a part of
this compilation. Is this a mistake for
"file:/I:/project/src/main/resources/jaxb_binding.xml"? -> [Help 1]
如果我从 customBindingFile.xjb 中删除 schemaLocation 属性,它不起作用并给出错误:
XPath evaluation of "//xs:complexType[@name='MyClass']" results in
empty target node
所以我的问题是如何避免在 customBindingFile.xjb 中提供特定的架构文件 name/location 并将其应用于 xsd ] 它用于生成 类.
在我们的架构师的帮助下,我解决了这个问题。我添加了一个 jaxws 绑定文件并在其中使用了无前缀的 xpath 查询来匹配我的请求元素。
这样我就不需要在任何地方提供模式位置,它将根据 XPath 查询应用于特定的 WSDL。
jaxws_binding_inheritance.xml
<jaxws:bindings version="2.0" xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance" jaxb:extensionBindingPrefixes="inheritance xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1">
<jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
<jaxws:bindings
node="*[local-name()='definitions']/*[local-name()='types']/*[local-name()='schema' and
(@targetNamespace='urn:net:mycompany:api:abc')]">
<jaxb:bindings
node="//*[local-name()='element' and
not(@name = 'ExcludeThisRequest' or @name = 'AlsoExcludeThisRequest') and
(substring(@name, string-length(@name) - string-length('Request') +1) = 'Request')]/*[local-name()='complexType']">
<inheritance:implements>com.kuldeep.CommonRequest</inheritance:implements>
</jaxb:bindings>
</jaxws:bindings>
</jaxws:bindings>
并在 wsdloption
下为我想应用它的 wsdl 添加了 jaxws 绑定文件 (jaxws_binding_inheritance.xml)。
pom.xml
<wsdlOption>
<wsdl>${project.build.directory}/generated/framework/cxf/abc-api-inline.wsdl</wsdl>
<bindingFiles>
<bindingFile>src/main/resources/jaxws_binding_inheritance.xml</bindingFile>
</bindingFiles>
</wsdlOption>
我正在使用 JAXB 从我的 XSD 文件生成 类。我想让生成的 类 实现一个通用接口。因此,我正在尝试使用 external binding file 方法来执行 JAXB2 Basics 插件。这是我的自定义绑定文件:
customBindingFile.xjb
<?xml version="1.0"?>
<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"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jxb:extensionBindingPrefixes="xjc">
<jxb:bindings schemaLocation="abc-api.xsd">
<jxb:bindings node="//xs:complexType[@name='MyClass']">
<inheritance:implements>com.kuldeep.CommonInterface</inheritance:implements>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
以下是我用于生成源代码的 pom 文件中的 maven 插件: 请注意 我添加的注释 是我对现有插件条目所做的更改。
pom.xml
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.plugin.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<!-- **extensions and args added by me** -->
<extensions>
<extension>org.jvnet.jaxb2_commons:jaxb2-basics:0.9.2</extension>
</extensions>
<args>
<arg>-Xinheritance</arg>
</args>
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
<defaultOptions>
<bindingFiles>
<bindingFile>src/main/resources/jaxws_binding.xml</bindingFile>
<bindingFile>src/main/resources/jaxb_binding.xml</bindingFile>
</bindingFiles>
</defaultOptions>
<wsdlOptions>
......
<wsdlOption>
<wsdl>${project.build.directory}/generated/framework/cxf/abc-api-inline.wsdl</wsdl>
<!-- **bindingFile added by me** -->
<bindingFile>src/main/resources/customBindingFile.xjb</bindingFile>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<!-- **dependency added by me** -->
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.9.2</version>
</dependency>
</dependencies>
</plugin>
我遇到的问题是我的模式文件 abc-api.xsd 驻留在其他一些项目中,所以当我尝试执行 maven 安装以生成我的 类,我收到错误提示 abc-api.xsd 不是此编译的一部分。
[ERROR] Failed to execute goal org.apache.cxf:cxf-codegen-plugin:3.0.3:wsdl2java (generate-sources) on project : Execution generate-sources of goal org.apache.cxf:cxf-codegen-plugin:3.0.3:wsdl2java failed: file:/I:/project/src/main/resources/customBindingFile.xjb [9,56]: "file:/I:/project/src/main/resources/abc-api.xsd" is not a part of this compilation. Is this a mistake for "file:/I:/project/src/main/resources/jaxb_binding.xml"? -> [Help 1]
如果我从 customBindingFile.xjb 中删除 schemaLocation 属性,它不起作用并给出错误:
XPath evaluation of "//xs:complexType[@name='MyClass']" results in empty target node
所以我的问题是如何避免在 customBindingFile.xjb 中提供特定的架构文件 name/location 并将其应用于 xsd ] 它用于生成 类.
在我们的架构师的帮助下,我解决了这个问题。我添加了一个 jaxws 绑定文件并在其中使用了无前缀的 xpath 查询来匹配我的请求元素。 这样我就不需要在任何地方提供模式位置,它将根据 XPath 查询应用于特定的 WSDL。
jaxws_binding_inheritance.xml
<jaxws:bindings version="2.0" xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance" jaxb:extensionBindingPrefixes="inheritance xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1">
<jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
<jaxws:bindings
node="*[local-name()='definitions']/*[local-name()='types']/*[local-name()='schema' and
(@targetNamespace='urn:net:mycompany:api:abc')]">
<jaxb:bindings
node="//*[local-name()='element' and
not(@name = 'ExcludeThisRequest' or @name = 'AlsoExcludeThisRequest') and
(substring(@name, string-length(@name) - string-length('Request') +1) = 'Request')]/*[local-name()='complexType']">
<inheritance:implements>com.kuldeep.CommonRequest</inheritance:implements>
</jaxb:bindings>
</jaxws:bindings>
</jaxws:bindings>
并在 wsdloption
下为我想应用它的 wsdl 添加了 jaxws 绑定文件 (jaxws_binding_inheritance.xml)。
pom.xml
<wsdlOption>
<wsdl>${project.build.directory}/generated/framework/cxf/abc-api-inline.wsdl</wsdl>
<bindingFiles>
<bindingFile>src/main/resources/jaxws_binding_inheritance.xml</bindingFile>
</bindingFiles>
</wsdlOption>