Java 如何展平生成的wsimport对象

Java 如何展平生成的wsimport对象,java,jaxb,wsimport,Java,Jaxb,Wsimport,我创建了基于java的web服务,并注意到wsimport处理jaxb参数/返回对象的有趣方式。当web方法返回jaxb对象时,wsimport将其创建为该方法的子对象(嵌套) 一个例子。。。假设我有一个名为GetRoles()的方法,它返回一个jaxb生成的名为RoleAdministration的对象。Wsimport生成以下嵌套对象:GetRoles.RoleAdministration 但是,如果相同的方法返回一个普通的旧序列化java对象,那么wsimport将返回它作为RoleAdm

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

一个例子。。。假设我有一个名为GetRoles()的方法,它返回一个jaxb生成的名为RoleAdministration的对象。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>

com.server
服务器网络
1.0-快照
4.0.0
战争
服务器网络
org.apache.maven.plugins
maven编译器插件
2.5.1
1.8
1.8
org.apache.maven.plugins
maven战争插件
假的
src
WEB-INF
jax-ws-catalog.xml
wsdl/**
org.apache.maven.plugins
maven依赖插件
2.1
验证
复制
${annowed.dir}
真的
爪哇
javaee认可的api
6
罐子
org.jvnet.jax-ws-commons
jaxws-maven插件
2.2
javax.xml
webservicesapi
1.4
${project.build.directory}/generated sources/jaxws-wsimport
真的
真的
真的
${basedir}/src/jax-ws-catalog.xml
泽西岛
球衣核心
1.8
罐子
爪哇
JavaEEAPI
7
假如
org.glassfish.metro
Web服务rt
2.3
假如

编辑:第一个块是同一对象在没有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>

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


reference

如果您发布了WSDL,这将非常有用,因为这可能不是一个可以通过wsimport解决的问题,而是在生成WSDL的过程中需要更改的问题。您的web服务的[简化]示例也会有所帮助。如果您所说的是真的,那么为什么生成的结果与wsimport不一致?唯一改变的因素是是否使用jaxb对象。此时,我无法发布wsdl。我可以告诉你,它在一个maven项目中,使用metro 2.3。我可以发布我的pom:就像我说的,我认为问题不在于wsimport。您应该检查WSDL,看看JAXB对象与其他对象相比是如何表示的,然后再从这里开始。谢谢。我将看一看wsdl对象的不同之处。我想你是对的。与另一个对象(除去注释)相同的jaxb注释对象如何会导致不同的行为?我正在使用javax.jws包中的WebService、WebMethod和WebParam注释。JAX-WS是SOAP的Java标准,它将JAXB用于XML片段。因此,具有JAXB注释的对象可以以不同的方式呈现,这是有意义的。事实上,通常使用JAXB注释来控制对象在WSDL中的表示方式。例如,为了防止显示字段,可以使用@javax.xml.bind.annotation.XMLTransient对字段进行注释,这是JAXB注释。