179

当我通过 maven 使用 CXF 中的 wsdl2java(生成类似于 wsimport 的东西)生成 Web 服务客户端时,我的服务以如下代码开始:

@WebServiceClient(name = "StatusManagement", 
                  wsdlLocation = "c:/some_absolute_path_to_a_wsdl_file.wsdl",
                  targetNamespace = "http://tempuri.org/") 
public class StatusManagement extends Service {

    public final static URL WSDL_LOCATION;
    public final static QName SERVICE = new QName("http://tempuri.org/", "StatusManagement");
    public final static QName WSHttpBindingIStatus = new QName("http://tempuri.org/", "WSHttpBinding_IStatus");
    static {
        URL url = null;
        try {
            url = new URL("c:/some_absolute_path_to_a_wsdl_file.wsdl");
        } catch (MalformedURLException e) {
            System.err.println("Can not initialize the default wsdl from c:/some_absolute_path_to_a_wsdl_file.wsdl");
            // e.printStackTrace();
        }
        WSDL_LOCATION = url;
    }

硬编码的绝对路径真的很烂。生成的类在我以外的任何其他计算机上都不起作用。

第一个想法是将 WSDL 文件(以及它导入的所有内容、其他 WSDL 和 XSD)放在 jar 文件和类路径中的某个位置。但我们想避免这种情况。由于所有这些都是由基于 WSDL 和 XSD 的 CXF 和 JAXB 生成的,我们认为在运行时需要知道 WSDL 没有任何意义。

wsdlLocation 属性旨在覆盖 WSDL 位置(至少这是我在某处读到的),它的默认值为“”。由于我们使用的是 maven,因此我们尝试<wsdlLocation></wsdlLocation>在 CXF 的配置中包含以尝试强制源生成器将 wsdlLocation 留空。但是,这只是让它忽略 XML 标记,因为它是空的。我们做了一个非常丑陋可耻的黑客攻击,使用<wsdlLocation>" + "</wsdlLocation>.

这也改变了其他地方:

@WebServiceClient(name = "StatusManagement", 
                  wsdlLocation = "" + "",
                  targetNamespace = "http://tempuri.org/") 
public class StatusManagement extends Service {

    public final static URL WSDL_LOCATION;
    public final static QName SERVICE = new QName("http://tempuri.org/", "StatusManagement");
    public final static QName WSHttpBindingIStatus = new QName("http://tempuri.org/", "WSHttpBinding_IStatus");
    static {
        URL url = null;
        try {
            url = new URL("" + "");
        } catch (MalformedURLException e) {
            System.err.println("Can not initialize the default wsdl from " + "");
            // e.printStackTrace();
        }
        WSDL_LOCATION = url;
    }

所以,我的问题是:

  1. 即使所有类都是由 CXF 和 JAXB 生成的,我们真的需要一个 WSDL 位置吗?如果是,为什么?

  2. 如果我们真的不需要 WSDL 位置,那么让 CXF 不生成它并完全避免它的正确和干净的方法是什么?

  3. 我们可以用那个 hack 得到什么不好的副作用?我们仍然无法对其进行测试以了解会发生什么,因此如果有人可以提前说,那就太好了。

4

10 回答 10

223

今天终于找到了这个问题的正确答案。

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxf.version}</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration> 
                <sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
                <wsdlOptions>
                    <wsdlOption>
                        <wsdl>${project.basedir}/src/main/resources/wsdl/FooService.wsdl</wsdl>
                        <wsdlLocation>classpath:wsdl/FooService.wsdl</wsdlLocation>
                    </wsdlOption>
                </wsdlOptions>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
</plugin>

请注意,我已将值前缀wsdlLocationclasspath:。这告诉插件 wsdl 将位于类路径而不是绝对路径上。然后它会生成类似这样的代码:

@WebServiceClient(name = "FooService", 
                  wsdlLocation = "classpath:wsdl/FooService.wsdl",
                  targetNamespace = "http://org/example/foo") 
public class Foo_Service extends Service {

    public final static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName("http://org/example/foo", "Foo");
    public final static QName FooSOAPOverHTTP = new QName("http://org/example/foo", "Foo_SOAPOverHTTP");
    static {
        URL url = Foo_Service.class.getClassLoader().getResource("wsdl/FooService.wsdl");
        if (url == null) {
            java.util.logging.Logger.getLogger(Foo_Service.class.getName())
                .log(java.util.logging.Level.INFO, 
                     "Can not initialize the default wsdl from {0}", "classpath:wsdl/FooService.wsdl");
        }       
        WSDL_LOCATION = url;
    }

请注意,这只适用于 2.4.1 或更新版本的 cxf-codegen-plugin。

于 2012-03-26T16:10:28.363 回答
22

我们用

wsdlLocation = "WEB-INF/wsdl/WSDL.wsdl"

换句话说,使用相对于类路径的路径。

我相信在编组/解组期间验证消息可能需要在运行时使用 WSDL。

于 2011-01-13T16:05:49.440 回答
20

对于那些org.jvnet.jax-ws-commons:jaxws-maven-plugin在构建时使用 WSDL 生成客户端的用户:

  • 将 WSDL 放在您的src/main/resources
  • 不要前缀with _wsdlLocationclasspath:
  • wsdlLocation做前缀/

例子:

  • WSDL 存储在/src/main/resources/foo/bar.wsdl
  • 配置和jaxws-maven-plugin_<wsdlDirectory>${basedir}/src/main/resources/foo</wsdlDirectory><wsdlLocation>/foo/bar.wsdl</wsdlLocation>
于 2015-09-10T11:14:14.133 回答
10

1)在某些情况下,是的。如果 WSDL 包含诸如策略等指导运行时行为的内容,则在运行时可能需要 WSDL。不会为与政策相关的事物等生成工件。此外,在一些晦涩的 RPC/Literal 情况下,并非所有需要的命名空间都在生成的代码中输出(根据规范)。因此,他们需要 wsdl。虽然是晦涩的案例。

2)我认为类似的东西会起作用。什么版本的CXF?这听起来像一个错误。您可以在其中尝试一个空字符串(只有空格)。不确定这是否有效。也就是说,在您的代码中,您可以使用接受 WSDL URL 的构造函数并只传递 null。不会使用 wsdl。

3)只是上面的限制。

于 2010-12-15T22:03:14.377 回答
6

我能够生成

static {
    WSDL_LOCATION = null;
}

通过将 pom 文件配置为 wsdlurl 为空:

    <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <executions>
            <execution>
                <id>generate-sources</id>
                <phase>generate-sources</phase>
                <configuration>
                    <sourceRoot>${basedir}/target/generated/src/main/java</sourceRoot>
                    <wsdlOptions>
                        <wsdlOption>
                            <wsdl>${basedir}/src/main/resources/service.wsdl</wsdl>
                            <extraargs>
                                <extraarg>-client</extraarg>
                                <extraarg>-wsdlLocation</extraarg>
                                <wsdlurl />
                            </extraargs>
                        </wsdlOption>
                    </wsdlOptions>
                </configuration>
                <goals>
                    <goal>wsdl2java</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
于 2011-08-02T16:23:40.867 回答
5

CXF 3.1.7 更新

在我的情况下,我将 WSDL 文件放入src/main/resources并将此路径添加到我在 Eclipse 中的 Srouces(右键单击项目-> 构建路径 -> 配置构建路径...-> 源 [Tab] -> 添加文件夹)。

这是我的pom文件的样子,可以看出wsdlLocation 不需要任何选项:

       <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>${cxf.version}</version>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
                        <wsdlOptions>
                            <wsdlOption>
                                <wsdl>classpath:wsdl/FOO_SERVICE.wsdl</wsdl>
                            </wsdlOption>
                        </wsdlOptions>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

这是生成的服务。可以看出,URL 来自 ClassLoader 而不是来自 Absolute File-Path

@WebServiceClient(name = "EventService", 
              wsdlLocation = "classpath:wsdl/FOO_SERVICE.wsdl",
              targetNamespace = "http://www.sas.com/xml/schema/sas-svcs/rtdm-1.1/wsdl/") 
public class EventService extends Service {

public final static URL WSDL_LOCATION;

public final static QName SERVICE = new QName("http://www.sas.com/xml/schema/sas-svcs/rtdm-1.1/wsdl/", "EventService");
public final static QName EventPort = new QName("http://www.sas.com/xml/schema/sas-svcs/rtdm-1.1/wsdl/", "EventPort");
static {
    URL url = EventService.class.getClassLoader().getResource("wsdl/FOO_SERVICE.wsdl");
    if (url == null) {
        java.util.logging.Logger.getLogger(EventService.class.getName())
            .log(java.util.logging.Level.INFO, 
                 "Can not initialize the default wsdl from {0}", "classpath:wsdl/FOO_SERVICE.wsdl");
    }       
    WSDL_LOCATION = url;   
}
于 2016-10-17T14:51:49.680 回答
4

是否有可能避免使用 wsdl2java?您可以立即使用 CXF 前端 API 来调用您的 SOAP Web 服务。唯一的问题是您需要在客户端创建 SEI 和 VO。这是一个示例代码。

package com.aranin.weblog4j.client;

import com.aranin.weblog4j.services.BookShelfService;
import com.aranin.weblog4j.vo.BookVO;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class DemoClient {
    public static void main(String[] args){
        String serviceUrl = "http://localhost:8080/weblog4jdemo/bookshelfservice";
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(BookShelfService.class);
        factory.setAddress(serviceUrl);
        BookShelfService bookService = (BookShelfService) factory.create();

        //insert book
        BookVO bookVO = new BookVO();
        bookVO.setAuthor("Issac Asimov");
        bookVO.setBookName("Foundation and Earth");

        String result = bookService.insertBook(bookVO);

        System.out.println("result : " + result);

        bookVO = new BookVO();
        bookVO.setAuthor("Issac Asimov");
        bookVO.setBookName("Foundation and Empire");

        result = bookService.insertBook(bookVO);

        System.out.println("result : " + result);

        bookVO = new BookVO();
        bookVO.setAuthor("Arthur C Clarke");
        bookVO.setBookName("Rama Revealed");

        result = bookService.insertBook(bookVO);

        System.out.println("result : " + result);

        //retrieve book

        bookVO = bookService.getBook("Foundation and Earth");

        System.out.println("book name : " + bookVO.getBookName());
        System.out.println("book author : " + bookVO.getAuthor());

    }
}

您可以在此处查看完整教程http://weblog4j.com/2012/05/01/developing-soap-web-service-using-apache-cxf/

于 2013-03-29T04:49:31.993 回答
3

说真的,最佳答案对我不起作用。尝试了 cxf.version 2.4.1 和 3.0.10。并每次使用 wsdlLocation 生成绝对路径。

我的解决方案是使用 withwsdl2java中的命令。apache-cxf-3.0.10\bin\-wsdlLocation classpath:wsdl/QueryService.wsdl

细节:

    wsdl2java -encoding utf-8 -p com.jeiao.boss.testQueryService -impl -wsdlLocation classpath:wsdl/testQueryService.wsdl http://127.0.0.1:9999/platf/testQueryService?wsdl
于 2016-08-23T09:23:06.707 回答
3

@Martin Devlers 解决方案工作正常。为了完整起见,提供以下步骤:

  1. 将您的 wsdl 放入资源目录,例如:src/main/resource
  2. 在 pom 文件中,添加 wsdlDirectory 和 wsdlLocation(不要错过 / 在 wsdlLocation 的开头),如下所示。而 wsdlDirectory 用于生成代码,而 wsdlLocation 用于在运行时创建动态代理。

    <wsdlDirectory>src/main/resources/mydir</wsdlDirectory>
    <wsdlLocation>/mydir/my.wsdl</wsdlLocation>
    
  3. 然后在您的 java 代码中(使用无参数构造函数):

    MyPort myPort = new MyPortService().getMyPort();
    
  4. 这是 pom 文件中的完整代码生成部分,在生成的代码中带有流利的 api。

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>2.5</version>
    
    <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-fluent-api</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-tools</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>
    
    <executions>
        <execution>
            <id>wsdl-to-java-generator</id>
            <goals>
                <goal>wsimport</goal>
            </goals>
            <configuration>
                <xjcArgs>
                    <xjcArg>-Xfluent-api</xjcArg>
                </xjcArgs>
                <keep>true</keep>
                <wsdlDirectory>src/main/resources/package</wsdlDirectory>
                <wsdlLocation>/package/my.wsdl</wsdlLocation>
                <sourceDestDir>${project.build.directory}/generated-sources/annotations/jaxb</sourceDestDir>
                <packageName>full.package.here</packageName>
            </configuration>
        </execution>
    </executions>
    

于 2019-11-19T10:24:12.117 回答
0

我能够生成自动生成的

    static {
        WSDL_LOCATION = null;
    }

    <wsdlLocation>null</wsdlLocation>

虽然,这样做的目的对我来说似乎是有限的。我试图尽可能快地实现短暂的 WS 客户端,最大限度地减少初始化svc = new MyService(); binding = svc.getMySoapPort();,但现在我明白了

INFO: Creating Service {http://www.example.com/MyApi}MyApiService from class com.company.MyApiPortType

代替

INFO: Creating Service {http://www.example.com/MyApi}MyApiService from WSDL ...

看来,new MyService();无论如何都是昂贵的,所以我必须考虑重用它的长寿命并发实例......因此,WSDL 的运行时解析将不成问题。

于 2021-07-22T04:20:32.353 回答