JAX-WS Five Minute Tutorial
What you need to run this example:
- JDK 1.6
- Eclipse .
- Be Excited ;)
Note:- You can download the source code for this example from the resources section.
Developing WebService End Point
1) Open Eclipse, and create a java project "WS-Server".
2) Create WS-Service Endpoint Interface:
package juma.mohammad;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface Greeting {
@WebMethod String sayHello(String name);
}
3) Create WS-Service Endpoint Implementation class:
package juma.mohammad;
import javax.jws.WebService;
@WebService(endpointInterface = "juma.mohammad.Greeting")
public class GreetingImpl implements Greeting {
@Override
public String sayHello(String name) {
return "Hello, Welcom to jax-ws " + name;
}
}
4) Create Endpoint Publisher class:
package juma;
import javax.xml.ws.Endpoint;
import juma.mohammad.GreetingImpl;
public class WSPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/WS/Greeting",new GreetingImpl());
}
}
5) Run the WSPublisher…. Guess what .. your WebService is published..
Wow.. check your service wsdl http://localhost:8080/WS/Greeting?wsdl
Developing WebService Client :
1) Open eclipse and create a new java project WS-Client
2) As you know we need to generate the client stubs... but how?
open your command line, and enter the wsimport command:
CD %CLIENT_PROJECT_HOME%\src
wsimport –s . http://localhost:8080/WS/Greeting?wsdl
You will find 6 java classes generated, and compiled under src/juma/mohammad.
You can remove *.class files , no need for them :)
3) Now Lets create Client Class which will be dependent on the stubs:
package juma;
import juma.mohammad.Greeting;
import juma.mohammad.GreetingImplService;
public class Client {
public static void main(String[] args){
GreetingImplService service = new GreetingImplService();
Greeting greeting = service.getGreetingImplPort();
System.out.println("------->> Call Started");
System.out.println(greeting.sayHello("Ali"));
System.out.println("------->> Call Ended");
}
}
4) Run the Client Class.... the output should looks like:
------->> Call Started
Hello, Welcom to jax-ws Ali
------->> Call Ended
Congratulations.... you managed to develop jax-ws Endpoint , Client..
The next tutorial will be how to deploy your Web Service on Tomcat.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
Fayez Alrafeea replied on Mon, 2010/03/29 - 3:42am
Nice and very simple.
Thank you.
Sebastián Open replied on Mon, 2010/03/29 - 9:34pm
Sebastián Open replied on Mon, 2010/03/29 - 9:36pm
Rudi Flager replied on Tue, 2010/03/30 - 2:55am
Shifong Chang replied on Wed, 2010/03/31 - 3:54pm
Nirav Purohit replied on Thu, 2010/04/01 - 3:19pm
Thanks.
Staring up with JAX-WS has never been so easy!!
Mohammad Juma replied on Sun, 2010/04/04 - 1:14am
@Shifong Chang
Run it as normal java application , nothing special here ;)
Paul Hodor replied on Fri, 2010/04/09 - 3:48pm
Thanks, the tutorial was very helpful. I looked through several others, but the simplicity of this one makes the process much clearer.
One question: How do you control the server that is launched in step 4, such as stopping, restarting, etc?
Sam Bos replied on Tue, 2010/07/20 - 5:15pm
Rene Unternaehrer replied on Wed, 2010/12/01 - 9:46am
Mohammad Juma replied on Fri, 2010/12/03 - 2:42pm
@Rene
this article maybe useful for you
http://java.dzone.com/articles/jax-ws-deployment-five-minute
Deepak Sharma replied on Sat, 2010/12/25 - 12:54pm
Tom Healy replied on Mon, 2011/03/21 - 12:26pm
Hi Mohammad,
I deployed the webservice to tomcat (jakarta tomcat 5.0.27) and got the error...
Provider org.apache.xalan.processor.TransformerFactoryImpl not found
I removed the xml-apis.jar from common/endorsed and tried again this time with no error. Howver, when I trya and access the service with the url http://localhost:8080/greetingWS/greeting I get a http 404. Any idea what I am doing wrong?
Thanks Tom
Khaled Jawhar replied on Tue, 2011/05/10 - 4:49am
Nos Doughty replied on Tue, 2011/05/24 - 7:47am
Superb!
I was pulling my hair out trying to work out how to put a quick WS together. All the other tutorials I found were an exercise in frustration. This is exactly 100% perfect to get started quickly and go from there. Thank you!
Khaled Jawhar replied on Sat, 2011/06/04 - 5:19am
Ronald Tagoe replied on Wed, 2011/07/06 - 6:16am
Edwin Jaws replied on Sat, 2011/07/30 - 2:07pm
root@squezze:~/ws/juma/mohammad# javac GreetingImpl.java GreetingImpl.java:6: cannot find symbol symbol: class Greeting public class GreetingImpl implements Greeting { ^ GreetingImpl.java:8: method does not override or implement a method from a supertype @Override ^ 2 errors root@squezze:~/ws/juma/mohammad#Edwin Jaws replied on Sat, 2011/07/30 - 2:09pm
Roman Würsch replied on Wed, 2011/11/30 - 4:27am
KISS ---> As i like...
Thanks a lot.
Kumeta Tahat replied on Sun, 2012/02/05 - 9:14am
Mohammad Juma replied on Tue, 2012/03/27 - 7:58am
in response to:
Kumeta Tahat
Semika Siriwardana replied on Thu, 2012/05/10 - 11:25pm
I could not create the 'Client.java'. It has only set of *.class files with in the src folder.
When I write the following code,
GreetingImplService service = new GreetingImplService();
It gives compiler error. Please explain the things in fully.
Regards
Semika
Carla Brian replied on Tue, 2012/06/19 - 5:50pm
jaizon lubaton replied on Thu, 2012/06/21 - 3:35pm
Mohammad Juma replied on Tue, 2012/07/03 - 6:49am
in response to:
jaizon lubaton
Congratulations :)
you can refer to this tutorial for web service deployment on Tomcat :
http://java.dzone.com/articles/jax-ws-deployment-five-minute
Hamid Seleman replied on Mon, 2012/11/26 - 3:23pm
Hi there,
Great post! simple and importantly, it works.
btw, I have to issue command to generate the cliet stub differently as follows:
wsimport http://localhost:8080/WS/Greeting?wsdl -s .
regards,
Alexander Podkutin replied on Sun, 2013/02/24 - 1:47pm
Thank you, very helpfull!
Praveen Das replied on Fri, 2013/02/15 - 4:45am
Thank You, was simple and understandable.
For anybody who are using Maven Projects please use below command to Generate Classes -
ex:
1. Go to Project Home
2. wsgen -s src/main/java -d target/jax-ws-maven-poc/WEB-INF/classes -cp target/jax-ws-maven-poc/WEB-INF/classes com.hcl.jaxws.impl.SampleWSImpl
It creates the classes in package under src/main/java
Just in case it helps somebody :)
Regards,
PD
Amruta Mistry replied on Thu, 2013/02/28 - 4:24am
Hi ,
I have created Maven based project where i am looking for pom configuration which will help me to generate client.Because when i m trying to use wsimport it throws an error.
Thanks