The basic steps for building a web service:
- Code the implementation class
- Annotate the class
- Use wsgen to generate web service artifacts
- Deploy in Glassfish from Eclipse
1. Code the implementation class
First create a dynamic web project in Eclipse. I called my project webservice, but you call your project anything you like. Make sure the target runtime is Glassfish, and select Minimal Configuration in the configuration screen.
In the next screen, accept the default output folder or choose a custom one. Memorize the output folder, because we need this when we generate the web service artifacts.
You don't have to select the last checkbox to auto generate the web.xml deployment descriptor, but you'll need it when your application is also a web application.
Click finish, and your project is ready to use.
Create a new class in the newly created project. This will be the implementation class of the web service. I called mine ExampleWS.
2. Annotate the class
Now it's time to complete the class and use JAX-WS annotation to make it a valid web service.
package com.javaeenotes;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class ExampleWS {
@WebMethod
public int sum(int a, int b) {
return a + b;
}
@WebMethod
public int multiply(int a, int b) {
return a * b;
}
@WebMethod
public String greet(String name) {
return "Hello " + name + "!";
}
}
3. Use wsgen to generate web service artifacts
Start a command prompt and navigate to the root of the project directory. Make sure the JDK is in your PATH, or else you can't execute the wsgen command. Create a wsdl directory in WebContent/WEB-INF/ or else wsgen will complain. Use the wsgen command to generate the artifacts like this:
wsgen -classpath build/classes/ -wsdl
-r WebContent/WEB-INF/wsdl -s src
-d build/classes/ com.javaeenotes.ExampleWS
This command will generate the source, class files, and WSDL file for the web service implemented as com.javaeenotes.ExampleWS. When wsgen complains about not finding the class, you have to build the project first. When the command is successful, you can refresh the project and you'll find the artifacts in your project tree.
The generated WSDL-file needs some editting, before you can use it. Open the WSDL-file and search for REPLACE_WITH_ACTUAL_URL. Replace this string with the web service URL: http://127.0.0.1:8080/webservice/ExampleWSService, and save the file.
4. Deploy in Glassfish from Eclipse
I assume you have Glassfish integrated in Eclipse, or else you have to build a WAR-file using ANT and manually deploy it into Glassfish. There is a blog post about integrating Glassfish into Eclipse. You can use the search engine to find it.
Deploy the project in Glassfish by right-clicking the project, click Run As, and finally Run on Server. Select the Glassfish server in the selection screen, and complete this step. When the configuration is finished, Eclipse tries to open a web page, which fails with a 404 error. Don't worry, this is normal, because we just created a web application without pages. Our project contains only a web service.
We can test the newly created web service by using SOAP-UI or the integrated Web Services Explorer in Eclipse. To use the Web Sercice Explorer in Eclipse, browse to the WSDL-file, right-click on it, select Web Services, and click on Test with Web Services Explorer. This will present you the web services test screen, which is pretty easy to use.
Nice Tutorial. Thank you!
ReplyDeleteCan Eclipse expose ejb stateless bean as web-servise ?
ReplyDeleteThank you in advance.
Hi, thank you for this great tutorial.
ReplyDeleteHowever I have always the same mistake; when I try to Press "Go" for any method I always get the error:
IWAB0135E An unexpected error has occurred.
java.net.ConnectException
Connection refused: connect
Do you have any idea in order to solve it?
Seems like a connection error. Did you specify the correct URL and port number?
ReplyDeleteI seem to be getting 404 Not Found when I try to test, any ideas? I followed the steps as listed here using eclipse helios.
ReplyDeleteAre you using Glassfish as application server?
ReplyDeleteI got the below error when i try to develop the application using above steps.
ReplyDeleteI am using Tomcat 6
IWAB0135E An unexpected error has occurred.
404
Not Found
Shekar, how did you deploy it on Tomcat?
ReplyDeleteI m using Tomacat7 which is configured in eclipse and i m also getting error
ReplyDeleteIWAB0135E An unexpected error has occurred.
404
Not Found
while passing the parameter and Go.
Do i have to do anything specific in my Tomcat configuration
-Pankaj
Pankaj
DeleteI don't know if you fixed this issue, but I am getting same error. However I tested it from the browser directly and it worked. In the servers window, expand GlassFish Management > Deployed Web Service. Right click on your web service and select "Test Web service in browser". You can also put the URL in the browser, like http://localhost:8080/CalcWSProject/CalcWSService?Tester. Note that I named it as CalcWS.
Dipak
Thanks bro.
ReplyDeletePlease help me to deploy this webservice on Tomcat7
ReplyDeleteHi G-Force,
DeleteThis tutorial might be useful for you...
http://java.dzone.com/articles/jax-ws-deployment-five-minute
Thanks...
Hi,
ReplyDeleteI am a beginner and I try to set wsgen path in C:\eclipse-jee-indigo-win32-x86_64\eclipse\bin, but I fail to do so.
Can you help?
Thanks
Tom
I think you should try to set JAVA_HOME environment variable...
DeleteThanks...
Hi Gin-Lung Cheng,
ReplyDeleteReally helpful tutorial, just works perfectly... Thanks and keep up the good work...
Do you have a maven pom.xml for this?
ReplyDeleteHi, a clarification :
ReplyDeletewsgen -classpath build/ lasses/ -wsdl
-r WebContent/WEB-INF/wsdl -s src
-d build/classes/ com.javaeenotes.SampleWS
or
wsgen -classpath build/ lasses/ -wsdl
-r WebContent/WEB-INF/wsdl -s src
-d build/classes/ com.javaeenotes.ExampleWS
am I wrong ?
tnx
Paul.
Good point! It is ExampleWS. I will correct that in the post.
Deletehow to access wsdl .
ReplyDeletewhen i use
http://127.0.0.1:8080/webservice/ExampleWSService?wsdl
it gives a error
i used jboss6
ReplyDelete