开发者

Can jaxb2-basics be configured to specify an interface for a type variable of a generic object?

开发者 https://www.devze.com 2023-03-30 03:14 出处:网络
I am using jax-ws\'s wsimport to generate java classes from WSDLs. I am using the jaxb2-basics plugin to create toString, equals, hashCode, copy, and merge methods.

I am using jax-ws's wsimport to generate java classes from WSDLs. I am using the jaxb2-basics plugin to create toString, equals, hashCode, copy, and merge methods.

I would like for the generated classes to implement interfaces to insulate our code from the implementation if we ever need to change it.

I would like to use jaxb2's ability to generate classes that implement a specified interface with limited success. It is not enough for the classes to implement an interface. I want generated classes that are composed of other generated classes to use interfaces instead of implementations when it refers to those objects.

I am using an external bindings file to define the interface that a class should implement:

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

For instance, if I have a class called Bus and it has a class called Person in it, I want Bus to implement IBus, Person to implement IPerson, and Bus to have a member variable of type IPerson, not Person.

public Class Bus implements IBus {

    protected IPerson driver;
    ...
}

What I have in the bindings file is working.

But, if Bus has a List of Person objects, I cannot get wsimport to generate a class for Bus that defines a List of IPerson objects.

public Class Bus implements IBus {

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

Here is the example wsdl I used (silly and stupid example, but it gets the point across):

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

Here is my 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>
     开发者_C百科 <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>

Does anyone know if it is possible to get jaxb2-basics to generate a generic object whose type variable is specified in a jaxb bindings file?


No, it is not possible at the moment.

UPDATE

This has now been implemented in 0.6.4.

0

精彩评论

暂无评论...
验证码 换一张
取 消