简体   繁体   English

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

[英]JAXWS: How to change CXF-generated classes names defined in external XSD?

I'm trying to change name of a class generated from wsdl (I don't want to modify any wsdl or xsd file directly). 我正在尝试更改从wsdl生成的类的名称(我不想直接修改任何wsdl或xsd文件)。 The problem is that its definition is in a separate xsd file. 问题在于它的定义在单独的xsd文件中。

The structrure of wsdl looks like this: wsdl的结构如下所示:

main.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: 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: 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>    

Now let's say I want to rename the FooType class to Foo. 现在,假设我想将FooType类重命名为Foo。 After reading this: JAXB: How to change XJC-generated classes names when attr type is specified in XSD? 读完这篇文章之后: JAXB:当在XSD中指定attr类型时,如何更改XJC生成的类名称? I've created a following bindings file. 我创建了以下绑定文件。

jaxws_bindings.xml: 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>

But all I get is an error: 但是我得到的只是一个错误:

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

I've already tried everything that came to my mind, but still got nothing close to a success. 我已经尝试了所有想到的方法,但是仍然没有成功的方法。 Anyone happens to know how to do this? 有人碰巧知道该怎么做吗?

PS: To generate the classes I use maven cxf codegen plugin with a following configuration in pom.xml: 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>

I figured this out based on this gist . 我是根据这个要点弄清楚的。

While this works with XJC: 虽然这适用于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>

you need this with CXF: 您需要使用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>

The difference is element vs complexType . 区别在于element vs complexType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM