如何避免在CXF或JAX-WS生成的Web服务客户端中指定WSDL位置? [英] How to avoid the need to specify the WSDL location in a CXF or JAX-WS generated webservice client?

查看:176
本文介绍了如何避免在CXF或JAX-WS生成的Web服务客户端中指定WSDL位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用来自CXF的wsdl2java生成一个web服务客户端(生成类似于wsimport的东西)时,通过maven,我的服务从这样的代码开始:

When I generate a webservice client using wsdl2java from CXF (which generates something similar to wsimport), via maven, my services starts with codes like this:

@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;
    }

硬编码的绝对路径真的很糟糕。生成的类不能在我的其他计算机上工作。

The hardcoded absolute path really sucks. The generated class won't work in any other computer other than mine.

第一个想法是放入WSDL文件(加上它导入的所有内容,其他WSDL和XSD)在jar文件和类路径中的某个地方。但我们想避免这种情况。由于所有这些都是由基于WSDL和XSD的CXF和JAXB生成的,因此我们认为无需在运行时知道WSDL。

The first idea is to put the WSDL file (plus everything it imports, other WSDLs and XSDs) somewhere in a jar-file and classpath it. But we want to avoid this. Since all that thing was generated by CXF and JAXB based in the WSDLs and XSDs, we see no point in needing to know the WSDL at runtime.

wsdlLocation属性是预期的覆盖WSDL位置(至少这是我在某处的内容),默认值为。由于我们使用的是maven,我们尝试在CXF配置中包含< wsdlLocation>< / wsdlLocation> ,以尝试强制源生成器将wsdlLocation留空。但是,这只是使它忽略XML标记,因为它是空的。我们做了一个非常难看的可耻黑客,使用< wsdlLocation>+< / wsdlLocation>

The wsdlLocation attribute is intended to override the WSDL location (at least this is what i readed somewhere), and it default value is "". Since we are using maven, we tried to include <wsdlLocation></wsdlLocation> inside the configuration of CXF to try to force the source generator to leave the wsdlLocation blank. However, this simply makes it ignore the XML tag because it is empty. We did a really ugly shameful hack, using <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位置?如果是,为什么?

  1. Does we really need a WSDL location even if all the classes were generated by CXF and JAXB? If yes, why?

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

If we do not really need the WSDL location, what is the proper and clean way to make CXF not generate it and avoiding it entirely?

我们可以通过该黑客获得哪些不良副作用?我们仍然无法测试看看会发生什么,所以如果有人可以提前说,那就太好了。

What bad side effects we could get with that hack? We still can't test that to see what happens, so if someone could say in advance, it would be nice.


推荐答案

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

I finally figured out the right answer to this question today.

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

请注意,我在 wsdlLocation 使用 classpath:。这告诉插件wsdl将在类路径而不是绝对路径上。然后它将生成类似于此的代码:

Notice that I have prefixed the value in wsdlLocation with classpath:. This tells the plugin that the wsdl will be on the classpath instead of an absolute path. Then it will generate code similar to this:

@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。

Note that this only works with version 2.4.1 or newer of the cxf-codegen-plugin.

这篇关于如何避免在CXF或JAX-WS生成的Web服务客户端中指定WSDL位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆