java 直接从源创建 Web 服务客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2131001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:36:45  来源:igfitidea点击:

Creating a web-service client directly from the source

javamaven-2wsdljax-ws

提问by benji

I am trying to generate the WS client jar directly from the @Webservice class(es).

我正在尝试直接从@Webservice 类生成 WS 客户端 jar。

Let's take this example :

让我们以这个例子为例:

   package com.example.maven.jaxws.helloservice;
   import javax.jws.WebService;
   @WebService
   public class Hello {
     public String sayHello(String param) {
     ;  return "Hello " + param;
     }
   }  

I can generate a war file and use glassfish to serve this webservice, and from there I can use the glassfish WSDL URL to generate the client sources.

我可以生成一个 war 文件并使用 glassfish 来为这个 web 服务提供服务,然后我可以使用 glassfish WSDL URL 来生成客户端源。

What I am trying to do is to skip the glassfish part. From my maven project defining the webservice, I would like to use the jaxws-maven-plugin to create the client classes but I cannot find any way to specify the actual URL of the webservice.

我想要做的是跳过 glassfish 部分。从我定义 web 服务的 maven 项目中,我想使用 jaxws-maven-plugin 创建客户端类,但我找不到任何方法来指定 web 服务的实际 URL。

It should be possible right?

应该可以吧?

@see also Creating a web-service client with a known but inaccessible wsdl

@see 还创建具有已知但无法访问的 wsdl 的 Web 服务客户端

采纳答案by rochb

You should use <wsdlLocation>option to give the location of the service where the WSDL file is going to be available after deployment.

您应该使用<wsdlLocation>option 来指定部署后 WSDL 文件将可用的服务位置。

Using -wsdlLocation switch

There is another easy way to do it - just run wsimport with -wsdlLocation switch and provide the WSDL location value which is relative to the generated Service class and you need to put this WSDL file at this relative location.

使用 -wsdlLocation 开关

还有另一种简单的方法 - 只需使用 -wsdlLocation 开关运行 wsimport 并提供与生成的服务类相关的 WSDL 位置值,您需要将此 WSDL 文件放在此相对位置。

See the post for more details.

有关详细信息,请参阅帖子

回答by Pascal Thivent

Creating a web service client application always starts with an existing WSDL file (unlike developing a web service provider) and, even if this is not the only way, I'd suggest to use the wsimporttool (see 5 Techniques for Creating Java Web Services from WSDLfor other options but I won't cover them).

创建 Web 服务客户端应用程序总是从现有的 WSDL 文件开始(与开发 Web 服务提供者不同),即使这不是唯一的方法,我也建议使用该wsimport工具(参见5 Techniques for Creating Java Web Services from WSDL用于其他选项,但我不会介绍它们)。

So, in your client project, add the following snippet to your pom.xml:

因此,在您的客户端项目中,将以下代码段添加到您的pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>jaxws-maven-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>wsimport</goal>
          </goals>
          <configuration>
            <wsdlUrls>
              <wsdlUrl>
                http://localhost:8080/helloservice/HelloService?wsdl
              </wsdlUrl>
            </wsdlUrls>
            <packageName>com.example.maven.jaxws.helloclient</packageName>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
<bulid>

The jaxws-maven-plugin:wsimportmojo is bound by default to the generate-sourceslife cycle phase so running any phase posterior to generate-sourceswill trigger the jaxws:wsimportgoal.

jaxws-maven-plugin:wsimport魔咒默认绑定的generate-sources生命周期阶段,因此运行的任何阶段后,以generate-sources将触发jaxws:wsimport目标。

Note that this is really a minimal configuration. If you want more details/control, check the documentation of the wsimportmojo.

请注意,这实际上是一个最小配置。如果您想要更多详细信息/控制,请查看wsimportmojo的文档。

For example, to use files instead of URLs for the WSDL (and to generate Java code in a location more compliant with maven best practices), use:

例如,要为 WSDL 使用文件而不是 URL(并在更符合 maven 最佳实践的位置生成 Java 代码),请使用:

<configuration>
  <sourceDestDir>${project.build.directory}/generated-sources/wsimport</sourceDestDir>
  <wsdlDirectory>${basedir}/src/wsdl</wsdlDirectory>
  <wsdlFiles>
    <wsdlFile>foo.wsdl</wsdlFile>
    <wsdlFile>bar.wsdl</wsdlFile>
  </wsdlFiles> 
  ...
</configuration>

Update:To invoke a pre-configured stub (using the endpoint address from the WSDL), the code is:

更新:要调用预先配置的存根(使用来自 WSDL 的端点地址),代码是:

Hello port = new HelloService().getHelloPort();
String result = port.sayHello("Duke!");

In order to invoke an endpoint whose address is different from the one specified in the WSDL, define the new endpoint URL and the QName:

为了调用地址与 WSDL 中指定的地址不同的端点,请定义新的端点 URL 和 QName:

URL endpoint_new = new URL( "NEW_ADDRESS_HERE" );
QName qname = new QName( "http://"+"ORIGINAL_PACKAGE", "SERVICENAME" );
Hello port = new HelloService( endpoint_new, qname ).getHelloPort();

where ORIGINAL_PACKAGEis the package where the service published in, SERVICENAMEis the name of the service we need, for example, HelloService.

其中ORIGINAL_PACKAGE是发布服务所在的包,SERVICENAME是我们需要的服务的名称,例如HelloService.