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
.