JAXWS:如何更改外部XSD中定义的CXF生成的类名?

时间:2015-07-20 13:56:39

标签: java wsdl cxf-codegen-plugin

我试图更改从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:要生成类,我在pom.xml中使用maven cxf codegen插件,配置如下:

<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>

1 个答案:

答案 0 :(得分: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