【问题标题】:How to flatten generated wsimport objects如何展平生成的 wsimport 对象
【发布时间】:2015-08-11 19:48:34
【问题描述】:

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

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

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

A) 为什么 jaxb 生成的对象会改变 wsimport 生成它的方式? B) 有什么特别的东西可以强制 wsimport 'flatten' 参数并返回对象吗?

根据要求:

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

【问题讨论】:

  • 如果您发布 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 注释。

标签: java jaxb wsimport


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 2012-08-30
    • 2012-06-27
    • 2016-09-12
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多