Creating a web-service client directly from the source

此生再无相见时 提交于 2019-12-03 16:11:12

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

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.

See the post for more details.

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 wsimport tool (see 5 Techniques for Creating Java Web Services from WSDL for other options but I won't cover them).

So, in your client project, add the following snippet to your 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:wsimport mojo is bound by default to the generate-sources life cycle phase so running any phase posterior to generate-sources will trigger the jaxws:wsimport goal.

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

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:

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

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:

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_PACKAGE is the package where the service published in, SERVICENAME is the name of the service we need, for example, HelloService.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!