In this tutorial, I'll show you how to use JAX-WS to build a web service in Eclipse. JAX-WS stands for Java API for XML Web Services. You can use it to build web services and clients that communicate using XML messages. We are not using the built-in web service generation tool provided by Eclipse. How to build a sample client will be explained in a future blog post.
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 classFirst 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 classNow 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 artifactsStart 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 EclipseI 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.