如何展平生成的wsimport对象

时间:2015-08-11 19:48:35

标签: java jaxb wsimport

我已经创建了基于Java的Web服务,并注意到wsimport处理jaxb参数/返回对象的一种有趣方式。当web方法返回一个jaxb对象时,wsimport将其创建为该方法的子对象(嵌套)。

一个例子......让我们说我有一个名为GetRoles()的方法,它返回一个名为RoleAdministration的jaxb生成对象。 Wsimport生成以下嵌套对象:GetRoles.RoleAdministration。

但是,如果同一方法返回一个普通的旧序列化java对象,它将被wsimport作为RoleAdministration返回。这对我来说似乎更好,因为我可以将其用于其他恰好将其用作参数或返回类型的Web方法。

A)为什么生成jaxb的对象改变了wsimport生成它的方式? B)是否有任何特殊的东西迫使wsimport“变平”'参数和返回对象?

根据要求:

<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">


<groupId>com.server</groupId>

<artifactId>Server-WEB</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>

<name>Server-WEB</name>

<properties>
</properties>
   <build>
      <plugins>
   <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>2.5.1</version>
       <configuration>
           <source>1.8</source>
           <target>1.8</target>
       </configuration>
   </plugin>
   <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-war-plugin</artifactId>
       <configuration>
           <failOnMissingWebXml>false</failOnMissingWebXml>
           <webResources>
               <resource>
                   <directory>src</directory>
                   <targetPath>WEB-INF</targetPath>
                   <includes>
                       <include>jax-ws-catalog.xml</include>
                       <include>wsdl/**</include>
                   </includes>
               </resource>
           </webResources>
       </configuration>
   </plugin>
   <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-dependency-plugin</artifactId>
       <version>2.1</version>
       <executions>
           <execution>
               <phase>validate</phase>
               <goals>
                   <goal>copy</goal>
               </goals>
               <configuration>
                   <outputDirectory>${endorsed.dir}</outputDirectory>
                   <silent>true</silent>
                   <artifactItems>
                       <artifactItem>
                           <groupId>javax</groupId>
                           <artifactId>javaee-endorsed-api</artifactId>
                           <version>6.0</version>
                           <type>jar</type>
                       </artifactItem>
                   </artifactItems>
               </configuration>
           </execution>
       </executions>
   </plugin>
   <plugin>
       <groupId>org.jvnet.jax-ws-commons</groupId>
       <artifactId>jaxws-maven-plugin</artifactId>
       <version>2.2</version>
       <executions></executions>
       <dependencies>
           <dependency>
               <groupId>javax.xml</groupId>
               <artifactId>webservices-api</artifactId>
               <version>1.4</version>
           </dependency>
       </dependencies>
       <configuration>
           <sourceDestDir>${project.build.directory}/generated-sources/jaxws-wsimport</sourceDestDir>
           <xnocompile>true</xnocompile>
           <verbose>true</verbose>
           <extension>true</extension>
           <catalog>${basedir}/src/jax-ws-catalog.xml</catalog>
       </configuration>
   </plugin>
     </plugins>
  </build>
  <dependencies>
     <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.8</version>
        <type>jar</type>
     </dependency>
     <dependency>
     <groupId>javax</groupId>
     <artifactId>javaee-api</artifactId>
     <version>7.0</version>
     <scope>provided</scope>
 </dependency>
 <dependency>
     <groupId>org.glassfish.metro</groupId>
     <artifactId>webservices-rt</artifactId>
     <version>2.3</version>
     <scope>provided</scope>
 </dependency>
  </dependencies>

编辑:第一个块是没有jaxb注释的相同对象的外观,第二个块是jaxb注释的外观。

<xs:complexType name="RoleResponse">
<xs:sequence>
<xs:element name="code" type="xs:int"/>
<xs:element name="message" type="xs:string" minOccurs="0"/>
<xs:element name="roleAdministration" type="tns:RoleAdministrationDSO"   minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="RoleAdministrationDSO">
<xs:sequence>
<xs:element name="dusfe" type="xs:string" minOccurs="0"/>
<xs:element name="roles" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Role" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="FunctionalArea" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>

这是jaxb版本:

        <xs:complexType name="RoleResponse">
    <xs:sequence>
    <xs:element name="code" type="xs:int"/>
    <xs:element name="message" type="xs:string" minOccurs="0"/>
    <xs:element name="roleAdministration" minOccurs="0">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="Roles">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="Role" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="FunctionalArea" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="Privledge" maxOccurs="unbounded">
    <xs:complexType>
    <xs:sequence/>
    <xs:attribute name="Name" type="xs:string"/>
    <xs:attribute name="Active" type="xs:boolean"/>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    <xs:attribute name="Name" type="xs:string"/>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    <xs:attribute name="Name" type="xs:string" use="required"/>
    <xs:attribute name="RoleID" type="xs:string"/>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    <xs:attribute name="dusfe" type="xs:string" use="required"/>
    <xs:attribute name="Version" type="xs:string" use="required"/>
    <xs:attribute name="Validation" type="xs:string" use="required"/>
    <xs:attribute name="ServerType" type="xs:int"/>
    <xs:attribute name="Message" type="xs:string"/>
    <xs:attribute name="Code" type="xs:int"/>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>

1 个答案:

答案 0 :(得分:0)

终于找到了有能力解决这个问题的人。两个重要的部分是在xsd响应/参数对象中设置名称空间,并直接在自己的方法上使用WebResult和Namespace注释。我遇到的奇怪行为直接来自wsimport与XmlRootElement的问题。

参考Why does wsimport have trouble with server object having @XmlRootElement annotation?