java - jaxb2-basics是否可以配置为泛型对象的类型变量指定接口(interface)?

标签 java jaxb jax-ws wsimport jaxb2-basics

我正在使用 jax-ws 的 wsimport 从 WSDL 生成 java 类。我正在使用 jaxb2-basics 插件来创建 toString、equals、hashCode、copy 和 merge 方法。

我希望生成的类能够实现接口(interface),以便在我们需要更改代码时将我们的代码与实现隔离开来。

我想使用 jaxb2 的功能来生成实现指定接口(interface)的类,但收效甚微。类实现接口(interface)是不够的。我希望由其他生成的类组成的生成类在引用这些对象时使用接口(interface)而不是实现。

我正在使用一个外部绑定(bind)文件来定义一个类应该实现的接口(interface):

<jaxws:bindings

xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
            xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
            xmlns:s="http://www.w3.org/2001/XMLSchema"
           xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
           wsdlLocation="ExampleService.wsdl"
           xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
           jaxb:extensionBindingPrefixes="inheritance">

<!-- Person -->
<jaxws:bindings node="wsdl:definitions/wsdl:types/s:schema[@targetNamespace='http://example.com/ws']">
    <jaxb:bindings node="//s:complexType[@name='Person']">
            <inheritance:implements>com.example.IPerson</inheritance:implements>
    </jaxb:bindings>
</jaxws:bindings>

<!-- Bus -->
<jaxws:bindings node="wsdl:definitions/wsdl:types/s:schema[@targetNamespace='http://example.com/ws']">
    <jaxb:bindings node="//s:complexType[@name='Bus']">
            <inheritance:implements>com.example.IBus</inheritance:implements>
            <jaxb:bindings node="//s:element[@name='Driver']">
                <jaxb:class ref="com.example.IPerson"/>
            </jaxb:bindings>
            <!-- How to do this? This doesn't work.
            <jaxb:bindings node="//s:element[@name='Passengers']">
                <jaxb:class ref="java.util.List&lt;com.example.IPerson&gt;"/>
            </jaxb:bindings>
             -->

    </jaxb:bindings>
</jaxws:bindings>

例如,如果我有一个名为 Bus 的类,其中有一个名为 Person 的类,我希望 Bus 实现 IBus,Person 实现 IPerson,Bus 有一个 IPerson 类型的成员变量,而不是 Person。

public Class Bus implements IBus {

    protected IPerson driver;
    ...
}

我在绑定(bind)文件中的内容正在运行。

但是,如果 Bus 有一个 Person 对象列表,我无法让 wsimport 为定义 IPerson 对象列表的 Bus 生成一个类。

public Class Bus implements IBus {

    protected IPerson driver;
    protected List<IPerson> passengers;
    ...
}

这是我使用的示例 wsdl(愚蠢而愚蠢的示例,但它明白了要点):

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:ns="http://example.com/ws"
xmlns:s1="http://schemas.xmlsoap.org/wsdl/soap/"
targetNamespace="http://example.com/ws"
name="BusServiceDefinitions">

<types>
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    attributeFormDefault="qualified"
    elementFormDefault="qualified"
    targetNamespace="http://example.com/ws">
    <xs:element name="BusRequest" type="ns:BusRequest"/>
    <xs:element name="BusResponse" type="ns:BusResponse"/>
    <xs:complexType name="Bus">
      <xs:sequence>
        <xs:element name="Driver" type="ns:Person" minOccurs="1" maxOccurs="1"/>
        <xs:element name="Passengers" type="ns:Person" minOccurs="1" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Person">
        <xs:sequence>
            <xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="BusRequest">
      <xs:sequence>
        <xs:element name="person" type="xs:string" minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
    </xs:complexType>
    <xs:complexType name="BusResponse">
      <xs:sequence>
        <xs:element name="bus" type="xs:string" minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
    </xs:complexType>
 </xs:schema>
</types>

<message name="BusRequest">
  <part name="BusRequest" type="ns:BusRequest"/>
</message>
<message name="BusResponse">
  <part name="Bus" type="ns:Bus"/>
</message>

<portType name="Bus_PortType">
  <operation name="createBus">
     <input message="ns:BusRequest"/>
     <output message="ns:BusResponse"/>
  </operation>
</portType>

<binding name="Bus_Binding" type="ns:Bus_PortType">
 <s1:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  <operation name="createBus">
     <s1:operation soapAction="createBus"/>
     <input>
     </input>
     <output>
     </output>
  </operation>
</binding>

<service name="Bus_Service">
  <documentation>WSDL File for HelloService</documentation>
  <port binding="ns:Bus_Binding" name="Bus_Port">
     <s1:address
        location="http://localhost:8080/soap/servlet/rpcrouter"/>
  </port>
 </service>
 </definitions>

这是我的pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>jaxws-test</groupId>
  <artifactId>jaxws-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>jaxws-test</name>
  <dependencies>
    <dependency>
      <groupId>com.sun.xml.ws</groupId>
      <artifactId>jaxws-rt</artifactId>
      <version>2.2.3</version>
    </dependency>
    <dependency>
      <groupId>org.jvnet.jaxb2_commons</groupId>
      <artifactId>jaxb2-basics</artifactId>
      <version>0.6.2</version>
    </dependency>
    <dependency>
      <groupId>org.jvnet.jaxb2_commons</groupId>
      <artifactId>jaxb2-basics-runtime</artifactId>
      <version>0.6.2</version>
    </dependency>
  </dependencies>
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
      <resource>
        <directory>src/main/wsdl</directory>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
           </execution>
         </executions>
          <configuration>
            <verbose>true</verbose>
            <wsdlOptions>
                <wsdlOption>
                    <wsdl>src/main/wsdl/ExampleService.wsdl</wsdl>
                    <extraargs>
                    <extraarg>-xjc-Xinheritance</extraarg>
                    </extraargs>
                    <bindingFiles>
                        <bindingFile>src/main/wsdl/ExampleBindings.xml</bindingFile>
                    </bindingFiles>
                </wsdlOption>
            </wsdlOptions>
          </configuration>
        <dependencies>
           <dependency>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>0.6.2</version>
            </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
        <id>maven2-repository.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository>
  </repositories>
</project>

有谁知道是否有可能让 jaxb2-basics 生成一个通用对象,其类型变量在 jaxb 绑定(bind)文件中指定?

最佳答案

不,目前不可能。

更新

这已经在 0.6.4 中实现了。

关于java - jaxb2-basics是否可以配置为泛型对象的类型变量指定接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7154563/

相关文章:

java - 如何在覆盖文件之前检查文件是否为空?

java - JTable 的 Customrenderer 被多次调用

java - 如何将 JAXB 对象编码到 org.w3c.dom.Document?

java - 通过 JSP 使用 Web 服务 JAX

tomcat - JAX-WS依赖导致tomcat Failed to scan File错误

java - 登录表单 IF-ELSE 无法正常工作

java - 是否有任何 API 可以从 Java 源文件生成包结构

java - 如何在有效的 SOAP 请求 xml 模板中填充值

java - JAXB 解码中的 NULL 值

jakarta-ee - 如何删除 clientgen 生成的 WS 客户端 jar 中的 JAXBElement<Integer>