【问题标题】:Controlling JAX-WS wsdlLocation attribute value's (absolute path) with jaxws-maven-plugin使用 jaxws-maven-plugin 控制 JAX-WS wsdlLocation 属性值(绝对路径)
【发布时间】:2012-03-21 10:16:35
【问题描述】:

我有一个 JAX-WS 驱动的 Web 服务,我们从另一个 Maven 模块(我称之为 ws-consumer)中生成一个 Web 服务客户端的 WSDL。

无论好坏,我们将“已发布的 WSDL”(服务在发布时持有/生成的 WSDL 和 XSD 的版本)复制到 ws-consumersrc/wsdl 文件夹,然后使用 jaxws-maven-plugin从 org.jvnet 使用 jaxws:wsimport 生成客户端,配置如下(截断):

    <plugin>
        <groupId>org.jvnet.jax-ws-commons</groupId>
        <artifactId>jaxws-maven-plugin</artifactId>
        <version>2.1</version>
        <executions>
            <execution>
                <!--phase>generate-sources</phase -->
                <goals>
                    <goal>wsimport</goal>
                </goals>
                <configuration>
                    <wsdlDirectory>src/main/resources/META-INF/wsdl/</wsdlDirectory>
                    <wsdlFiles>
                        <wsdlFile>MyWS/MyWS.wsdl</wsdlFile>
                    </wsdlFiles>
                </configuration>
            </execution>
        </executions>
    </plugin>

现在,生成的客户端代码在类级别应用了以下注释:

@WebServiceClient(name = "MyWS", targetNamespace = "http://myws/blah", wsdlLocation = "**file:/C:/some/absolute/path/src/main/resources/META-INF/wsdl/MyWS/MyWS.wsdl"**)

强调我的

如您所见,wsdlLocation 属性值有一个硬编码的绝对路径,在部署服务时该路径将不正确。

有什么方法可以通过将其设置为 META-INF/wsdl/MyWS/MyWS.wsdl 或其他值来“控制”它?

【问题讨论】:

    标签: java web-services maven wsdl jax-ws


    【解决方案1】:

    可以使用 Codehaus 插件:

    <plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>jaxws-maven-plugin</artifactId>
       <version>1.9</version>
       <executions>
         <execution>
           <goals>
             <goal>wsimport</goal>
           </goals>
         </execution>
       </executions>
       <configuration>
         <keep>true</keep>
         <verbose>true</verbose>
         <wsdlDirectory>../wscontract/src/main/resources/wsdl</wsdlDirectory>
         <wsdlLocation>wsdl/MaintainAddress.wsdl</wsdlLocation>
         <sourceDestDir>src/main/java</sourceDestDir>
         <bindingDirectory>.</bindingDirectory>
         <bindingFiles>
           <bindingFile>jaxb/xsdbindings.xml</bindingFile>
           <bindingFile>jaxb/wsdlbindings.xml</bindingFile>
         </bindingFiles>
       </configuration>
    </plugin>
    

    也许您正在使用的插件有类似的选项,或者您可以考虑切换。

    您也可以显式配置您的 WSDL,在这种情况下,此属性将被忽略,尽管这在容器管理的应用程序中可能不合适。

    示例代码here

    【讨论】:

    • 这基本上就是答案。 jaxws-maven-plugin 提供 wsdlLocation 可以通配符但覆盖生成的 WebServiceClient.wsdlLocation 值。谢谢!
    • @McDowell - 我认为 codehaus 插件已迁移到 jax-ws-commons。每jax-ws-commons.java.net/jaxws-maven-pluginThis plugin contains Maven adapter for JAX-WS's toolset. The original version of this was developed in the codehaus mojo project, but as of March 2007, the project is moved to jax-ws-commons.
    • @atc 你在 wsdlLocation 中放了什么?由于我们还尝试将其定位到 WSDL 所在的文件夹,因此通过 1)添加 src/main/wsdl 并且在这种情况下:我们有 WSDL 文件(Soap_1.wsdl ) 在生成的 WAR 中的 WEB-INF/类中 2) 只需放置 Soap_1.wsdl。但同样它不起作用。你是如何解决这个问题的?
    • @demaniak org.jvnet.jax-ws-commons:jaxws-maven-plugin:2.3 源自 Codehas 插件,在 jax-ws-commons.java.net/jaxws-maven-plugin/examples/… 处记录了如何将 wsimport 目标与多个 WSDL 一起使用
    • @SimeonAngelov 这确实有效,但前提是您将前面的“/”放入 wsdlLocation。所以当wsdlDirectory包含“src/main/resources/wsdl”时,wsdlLocation需要“/wsdl/SoapService.wsdl”
    【解决方案2】:

    wsdlLocation 与来自 org.jvnet.jax-ws-commons 的 jaxws-maven-plugin 一起使用:

    <plugin>
    <groupId>org.jvnet.jax-ws-commons</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <goals>
                <goal>wsimport</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
        <wsdlFiles>
            <wsdlFile>arsdev.wsdl</wsdlFile>
        </wsdlFiles>
        <wsdlLocation>wsdl/*</wsdlLocation>
        <!-- Keep generated files -->
        <keep>true</keep>
        <packageName>jaxws.remedy.client.generated</packageName>
        <!-- generated source files destination -->
        <sourceDestDir>target/generated-code/src</sourceDestDir>
    </configuration>
    </plugin>
    

    【讨论】:

    • 这终于对我有用了。但仅当您在 wsdlLocation 中提供前面的“/”时 - 像这样 /wsdl/*
    • 详情也可以在本地显示:mvn jaxws:help -Dgoal=wsimport -Ddetail
    • 感谢 @jonashackt 的 cmets。我在上面的答案中添加了编辑。
    【解决方案3】:

    我投票支持@dean-schulze 答案,因为它适合 org.jvnet.jax-ws-commons:jaxws-maven-plugin 插件的情况。

    使用 CLI 在本地显示帮助也可能很有趣,如下所示:

    mvn jaxws:help -Dgoal=wsimport -Ddetail
    

    如上一个答案所述,我们可以使用 wsdlLocation 参数,如下所述:

    wsdlLocation
      @WebService.wsdlLocation and @WebServiceClient.wsdlLocation value.
      Can end with asterisk in which case relative path of the WSDL will be
      appended to the given wsdlLocation.
    
      Example:
    
       ...
       <configuration>
       <wsdlDirectory>src/mywsdls</wsdlDirectory>
       <wsdlFiles>
       <wsdlFile>a.wsdl</wsdlFile>
       <wsdlFile>b/b.wsdl</wsdlFile>
       <wsdlFile>${basedir}/src/mywsdls/c.wsdl</wsdlFile>
       </wsdlFiles>
       <wsdlLocation>http://example.com/mywebservices/*</wsdlLocation>
       </configuration>
       ...
      wsdlLocation for a.wsdl will be http://example.com/mywebservices/a.wsdl
      wsdlLocation for b/b.wsdl will be
      http://example.com/mywebservices/b/b.wsdl
      wsdlLocation for ${basedir}/src/mywsdls/c.wsdl will be
      file://absolute/path/to/c.wsdl
    
    
      Note: External binding files cannot be used if asterisk notation is in
      place.
    

    -wsdllocation 选项也记录在 JDK 的 wsimport 命令中:

    但它只是说(见@WebServiceClient javadoc):

    Specifies the @WebServiceClient.wsdlLocation value.
    

    【讨论】:

    • 实际上我正在使用org.codehaus.mojo:jaxws-maven-plugin 但这个答案对我有帮助。谢谢。
    【解决方案4】:

    @dean-schultze 的答案是正确的,只是它尚未更新以反映@jonashackt 的评论,即wsdlLocation 需要以斜线为前缀才能相对于基数。即

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxws-maven-plugin</artifactId>
        <version>2.6</version>
        <executions>
            <execution>
                <goals>
                    <goal>wsimport</goal>
                </goals>
                <configuration>
                    <wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
                    <wsdlFiles>
                        <wsdlFile>some.wsdl</wsdlFile>
                    </wsdlFiles>
                    <wsdlLocation>/wsdl/*</wsdlLocation>
                    <packageName>com.example.generated</packageName>
                    <sourceDestDir>target/generated-sources/</sourceDestDir>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    还将 groupId 更新为 codehaus。堆栈溢出不允许我编辑原始帖子以尝试更正它

    【讨论】:

      【解决方案5】:

      1.12 版无法识别&lt;wsdlLocation&gt;。它抱怨:

       No WSDLs are found to process, Specify atleast one of the following parameters: wsdlFiles, wsdlDirectory or wsdlUrls.
      

      1.9 版(如您的示例中所示)只是忽略所有内容并且不生成任何代码。

      一定有什么改变了。

      【讨论】:

      • wsdlFileswsdlDirectorywsdlUrls 参数之一是必需的,因为这些参数指定一个或多个 WSDL 文件的“物理”位置。 wsdlLocation 用于@WebServiceClient.wsdlLocation 注解的值only
      • @FlorianWolters 如何在 android 后端模块中设置 wsdl 文件位置 url?我试图将它放在src\main\java\webapp 甚至....\webapp\WEB-INF 目录中,但我收到此错误javax.xml.ws.WebServiceException: Cannot find './src/main/java/webapp/WEB-INF/Checkout.wsdl' wsdl. Place the resource correctly in the classpath.
      • @gikarasojo kinene 我没有Android开发经验,但如果你使用Maven,那么程序应该是一样的。我假设您想创建一个 JAX-WS 客户端?我建议阅读(并查看源代码)以下文章:docs.oracle.com/javaee/7/tutorial/jaxws001.htm
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      相关资源
      最近更新 更多