【问题标题】:JAXWS: How to change CXF-generated classes names defined in external XSD?JAXWS:如何更改在外部 XSD 中定义的 CXF 生成的类名称?
【发布时间】:2015-10-09 16:31:03
【问题描述】:

我正在尝试更改从 wsdl 生成的类的名称(我不想直接修改任何 wsdl 或 xsd 文件)。问题是它的定义在一个单独的 xsd 文件中。

wsdl的结构是这样的:

main.wsdl:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://foo.bar/zee">
<wsdl:import location="typedef.wsdl" namespace="http://foo.bar/wee">
</wsdl:import>
    ...
</wsdl:definitions>

typedef.wsdl:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Foo" targetNamespace="http://foo.bar/wee">
    <wsdl:types>
        <xsd:schema>
            <xsd:import namespace="http://foo.bar/wee/schema" schemaLocation="FooBar.xsd"/>
        </xsd:schema>
    </wsdl:types>
    ...
<wsdl:definitions> 

FooBar.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://foo.bar/wee/schema">
    ...
    <xsd:complexType name="FooType">
    <xsd:sequence>
        ...
    </xsd:complexType>
</xsd:schema>    

现在假设我想将 FooType 类重命名为 Foo。阅读此内容后:JAXB: How to change XJC-generated classes names when attr type is specified in XSD? 我创建了以下绑定文件。

jaxws_bindings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
        version="2.1"
        wsdlLocation="http://127.0.0.1:8431/Foo/types.wsdl">

    <jxb:bindings schemaLocation="http://127.0.0.1:8431/Foo/FooBar.xsd">
        <jxb:bindings node="//xs:complexType[@name='FooType']">
            <jxb:class name="Foo"/>
        </jxb:bindings>
    </jxb:bindings>

</jxb:bindings>

但我得到的只是一个错误:

[ERROR] Failed to execute goal org.apache.cxf:cxf-codegen-plugin:2.7.0:wsdl2java (generate-sources) on
project foo: Execution generate-sources of goal org.apache.cxf:cxf-codegen-plugin:2.7.0:wsdl2java failed:
file:/E:/sources/.../jaxws_bindings.xml [8,95]: "http://127.0.0.1:8431/Foo/FooBar.xsd" is not a part of 
this compilation. Is this a mistake for "http://127.0.0.1:8431/Foo/FooBar.xsd"? -> [Help 1]

我已经尝试了所有想到的方法,但仍然没有成功。 有人碰巧知道怎么做吗?

PS:为了生成类,我使用了 maven cxf codegen 插件,在 pom.xml 中有以下配置:

<build>
    <finalName>${project.groupId}.${project.artifactId}</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>2.7.0</version>
        <executions>
          <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
              <sourceRoot>${project.build.directory}/generated-sources</sourceRoot>
              <wsdlOptions>
                <wsdlOption>
                  <wsdl>http://127.0.0.1:8431/Foo/main.wsdl</wsdl>
                  <extraargs>
                    <extraarg>-client</extraarg>
                  </extraargs>
                  <bindingFiles>
                    <bindingFile>jaxws_bindings.xml</bindingFile>
                  </bindingFiles>
                </wsdlOption>
              </wsdlOptions>
            </configuration>
            <goals>
              <goal>wsdl2java</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>

【问题讨论】:

    标签: java wsdl cxf-codegen-plugin


    【解决方案1】:

    我是根据this gist 得出这个结论的。

    虽然这适用于 XJC:

    <jaxb:bindings
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        version="1.0">
      <jaxb:bindings schemaLocation="..."
                     node="/xsd:schema/xsd:element[@name='Bar']">
        <jaxb:class name="Foo"/>
      </jaxb:bindings>
    </jaxb:bindings>
    

    CXF 需要这个:

    <jaxb:bindings
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        version="1.0">
      <jaxb:bindings schemaLocation="..."
                     node="/xsd:schema/xsd:complexType[@name='Case']">
        <jaxb:class name="Wat" implClass="bar"/>
      </jaxb:bindings>
    </jaxb:bindings>
    

    区别在于elementcomplexType

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-10
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      相关资源
      最近更新 更多