Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a web-service client directly from the source

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

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 web service, and from there I can use the glassfish WSDL URL to generate the client sources.

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.

It should be possible, right?

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

like image 393
benji Avatar asked Jan 25 '10 08:01

benji


2 Answers

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.

like image 161
Pascal Thivent Avatar answered Sep 18 '22 00:09

Pascal Thivent


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.

like image 20
rochb Avatar answered Sep 22 '22 00:09

rochb