Sunday, October 3, 2010

Web service with JAX-WS in Eclipse

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:

  1. Code the implementation class
  2. Annotate the class
  3. Use wsgen to generate web service artifacts
  4. 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.

21 comments:

  1. Can Eclipse expose ejb stateless bean as web-servise ?

    Thank you in advance.

    ReplyDelete
  2. Hi, thank you for this great tutorial.
    However 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?

    ReplyDelete
  3. Seems like a connection error. Did you specify the correct URL and port number?

    ReplyDelete
  4. I seem to be getting 404 Not Found when I try to test, any ideas? I followed the steps as listed here using eclipse helios.

    ReplyDelete
  5. Are you using Glassfish as application server?

    ReplyDelete
  6. I got the below error when i try to develop the application using above steps.
    I am using Tomcat 6
    IWAB0135E An unexpected error has occurred.
    404
    Not Found

    ReplyDelete
  7. Shekar, how did you deploy it on Tomcat?

    ReplyDelete
  8. I m using Tomacat7 which is configured in eclipse and i m also getting error
    IWAB0135E 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

    ReplyDelete
    Replies
    1. Pankaj

      I 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

      Delete
  9. Please help me to deploy this webservice on Tomcat7

    ReplyDelete
    Replies
    1. Hi G-Force,

      This tutorial might be useful for you...

      http://java.dzone.com/articles/jax-ws-deployment-five-minute

      Thanks...

      Delete
  10. Hi,
    I 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

    ReplyDelete
    Replies
    1. I think you should try to set JAVA_HOME environment variable...

      Thanks...

      Delete
  11. Hi Gin-Lung Cheng,

    Really helpful tutorial, just works perfectly... Thanks and keep up the good work...

    ReplyDelete
  12. Do you have a maven pom.xml for this?

    ReplyDelete
  13. Hi, a clarification :

    wsgen -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.


    ReplyDelete
    Replies
    1. Good point! It is ExampleWS. I will correct that in the post.

      Delete
  14. how to access wsdl .

    when i use
    http://127.0.0.1:8080/webservice/ExampleWSService?wsdl

    it gives a error

    ReplyDelete