Skip to main content Skip to complementary content

JAX-WS Development With Eclipse

About this task

Development using the Eclipse IDE is another option. The Java Enterprise Edition version of Eclipse allows for rapid development, testing, and debugging, making it a compelling environment for web service development. Steps to follow for building web services with Eclipse:

Procedure

  1. Download and attach Tomcat to Eclipse.
    Tomcat needs to be linked into the IDE so Eclipse can start and stop the servlet container as well as deploy web services to it. (See the Eclipse/Tomcat FAQ to learn more.) Download and extract the Tomcat binary into its own directory if you haven't already. Next, from the Eclipse Preferences window (Menu item Windows > Preferences), browse to Server > Runtime Environments and add your Tomcat installation to the IDE:

    Example

    Ensure that the JRE being used is an actual Java Development Kit (JDK), which Tomcat needs in order to compile JSPs and potentially other source files.
  2. Download and attach CXF to Eclipse.
    Similar to Tomcat, download the latest release version of Apache CXF and expand into a directory on your computer. Next, from the Eclipse Preferences, browse to Web Services > CXF 2.x Preferences, and enter the CXF directory as shown below.

    Example

    Also, in the Web Services > Server and Runtime category, make sure the Server and Web Service Runtimes are set to your version of Tomcat and CXF respectively.
  3. Create a dynamic web project.
    You will next create a Eclipse dynamic web project to contain the web service provider and client. From the Eclipse File menu, select New > Dynamic Web Project (or New > Other, and from the subsequent popup dialog Web > Dynamic Web Project). Give the project any desired name (WebServiceSample is used below), make sure the proper Tomcat server is selected, and then click Finish:

    Example

  4. Create the web service provider.
    Let's start from a WSDL-first approach.
    1. First you will create the WSDL. From the Eclipse Project Explorer, right-click WebServiceSample, select New > File, and enter DoubleIt.wsdl as the file name. Next, copy-and-paste the DoubleIt.wsdl from the CXF repository into this new file.
    2. Right-click WebServiceSample again and select New > Other, and Web Services > Web Service from the new dialog. Choose the Top down Java bean Web Service (i.e., WSDL-first) type and the DoubleIt.wsdl file you just created as the Service definition. Make sure the server and web service runtime (i.e., CXF) are correct. On the server thumbwheel, select the highest setting (Test service) as shown below.

      Example

      Click Finish. Eclipse will deploy the web service on Tomcat and provide a Web Services Explorer test window where you can enter numbers and see the doubled result, as shown below. However, the web service presently will just return a hard-coded number, you will need to modify the web service provider to return the desired result, which you will do next.

      Example

    3. From the Project Explorer, under WebServiceSample, open up Java Resources: src and open the DoubleItPortTypeImpl.java file. Change the business logic of the doubleIt method to return twice the input parameter (i.e., " int _return = numberToDouble * 2; "), and save the file. Once saved, Eclipse automatically recompiles the class and redeploys it to the Tomcat server. You can return to the Web Services Explorer test window, enter a number and now see the properly doubled result.
  5. Create the web service client.
    You will now use Eclipse to create a Java-based web service client. Right-click WebServiceSample from the Project Explorer and select New > Other, then Web Services > Web Service Client. From the window that appears enter the WSDL file and raise the thumbwheel all the way up to "test", as shown below. Then click Next and enter another package name for the client (org.example.doubleit.client for example) so it does not conflict with the web service provider package. Click Finish.

    Example

    Two modifications to the auto-generated client code will need to be made. First, in the org.example.doubleit.client.DoubleItService file, the wsdlLocation and url fields highlighted below will need to be updated to the actual endpoint URL Eclipse is using for the web service provider. You can find the value by looking at the wsdlLocation field in the DoubleItService class of the web service provider (it's also viewable in the Web Services Explorer test view).
    @WebServiceClient(name = "DoubleItService", 
       wsdlLocation = "http://localhost:8080/WebServiceSample/" 
       + services/DoubleItPort?wsdl",
       targetNamespace = "http://www.example.org/DoubleIt") 
       public class DoubleItService extends Service {
        
          public final static URL WSDL_LOCATION;
          public final static QName SERVICE =
             new QName("http://www.example.org/DoubleIt", 
                "DoubleItService");
          public final static QName DoubleItPort = 
             new QName("http://www.example.org/DoubleIt", 
                "DoubleItPort");
        
          static {
             URL url = null;
             try {
                url = new URL(
                   "http://localhost:8080/WebServiceSample/" + 
                   "services/DoubleItPort?wsdl");
                } catch (MalformedURLException e) {
                   System.err.println(
                      "Can not initialize the default wsdl from" 
                      + "http://localhost:8080/doubleit/services/doubleit?wsdl");
                   // e.printStackTrace();
                }
                WSDL_LOCATION = url;
             }
    ...
    Second, in the DoubleItPortType_DoubleItPort_Client class, modify the value of the _doubleIt_numberToDouble constant to a number you want to double (55 for example), and save your change. Then right-click the file in the Eclipse editor and select Run As > Java Application. You should see the result of the web service call (110) in the Eclipse Console window:
    INFO: Creating Service {http://www.example.org/DoubleIt}DoubleItService 
    from WSDL: http://localhost:8080/WebServiceSample/services/DoubleItPort?wsdl
    Invoking doubleIt...
    doubleIt.result=110

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!