Use Maven to Generate all Stubs using "wsimport" utility . After generating the webservice you can call the service using Java class. This method will can be easily reused , you just have to use different wsdl url every time you have to consume the webservice and modify the java code , it's light weight and no need to use any third party libraries . So you will need the following
wsdl file
pom.xml
Java Class
First step is to copy the wsdl file and place it in the src/wsdl directory
Setup the pom.xml
pom.xml Folder : Project root dir
run "mvn generate-sources" command , this will generate the files in src/main/java folder . Its specified as <sourceDestDir>src/main/java</sourceDestDir> in pom.xml
Stubs are generated ,You can call the webservice with following Java program
package com.myservice.client;
import com.myservice.proxy.MyService;
import com.myservice.proxy.MyServiceService;
public class CallMyService {
public static void main(String[] args) {
MyServiceService mss = new MyServiceService();
MyService serv = mss.getMyServicePort();
System.out.println(serv.sayHello());
}
}
Now that was simple right . So what if you want to generate numerous webservice clients for testing and scripting needs you can simply reuse the pom.xml ( change the wsdl file ) , generate the stubs and change the program to call the new webservice .
Note
If your webservice requires authentication , you can set that up as shown below
Article describes setting up JAX-WS using Maven and various ways to deploy the webservice . Creating WS using JAX-WS is super simple , simply annotate the class with @WebService and its basically done. Simpicity is good not only for development but also creating mock services and testing error cases.
Maven makes it more reusable be able to distibute easily to developers. Also maven takes care of wsgen and dependent libraries.
I use Tomcat 7 and running Java 6 . If you are using Java7 , wsgen fails ( yeah it has to be fixed) , so till then configure the IDE to use Java6 for the project
Java Code - Your WebService ( Simple .. basically you can return POJO or collection of objects for practical use case)
folder - src/main/java
package com.services;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class MyService {
@WebMethod
public String sayHello()
{
return "Hello World";
}
}
Running it using EndPoint
Using Endpoint just use the publisher and WebService is running .
package com.services;
import javax.xml.ws.Endpoint;
public class MyServicePublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:8888/TestWS/MyService", new MyService());
}
}
test service : http://localhost:888/TestWS/MyService?wsdl
Running it in Tomcat.
My preferred method is to run it in tomcat . You will need couple of more files
web.xml
folder :src/webapp/WEB-INF