Step by Step JAX-WS Web Services with Eclipse, TomEE, and Apache CXF

This is a step by step tutorial to try out JAX-WS web services using Eclipse, TomEE, and CXF. There will not be a need to use maven and all configuration and preparation will be done manually using Eclipse.

Sandbox Environment

  • Sandbox OS : Windows 7 64 bit
  • JDK : Java SE Development Kit 7. <Link>
  • IDE : Eclipse IDE for Java EE Developers, Version Juno <Link>
  • App Server: Apache TomEE is a Java EE application server built on top of Tomcat <Link>
  • JAX-WS Runtime: Apache CXF that is bundled in TomEE container. <Link>

Download and installation

  • Download “Windows x64” JDK 7 from here. Install JDK by double clicking on the downloaded .exe file.
  • Download Eclipse from here. Expand the zip folder onto a folder location of your choice. We will be referring to this location as “<Eclipse_home>”.
  • Download TomEE from here. Expand the zip folder onto a folder location of your choice. We will be referring to this location as “<TomEE_Home>”

Steps and Discussion

Steps and source code are developed mostly customizing the tutorial “JAX-WS @WebService example” provided at TomEE site at location http://tomee.apache.org/examples-trunk/simple-webservice/ .

Write Web Services Code

  • Start Eclipse IDE by clicking <Eclipse_Home>\eclipse.exe

01

  • Click File > New > Other…

02

  • Type in “dynamic Web Project” in Wizards input box. Dynamic Web Project wizard should show up for selection. Select “Dynamic Web Project”. Click “Next” button.

03

  • Type in “WebServicesExperiment” in Project name input box.

04

  • Click “New Runtime…” button. A “New Server Runtime Environment” dialog box shows up. Expand “Apache” and select “Apache Tomcat v7.0”. Click “Next” button.

05

  • Type in “TomEE Runtime” on “Name” input box. Click “Browse…” button to selection TomEE installed location which is being referred to as “<TomEE_Home>”. For example for the laptop that was used for this tutorial, <TomEE_Home> location is “S:\servers\apache-tomee-plus-1.5.1”. Click “Finish” button.

06

  • Click “Finish” button again on “New Dynamic Web Project” dialog box.
  • Select Window > Show View > Navigator. Your Eclipse window should look similar to below.

07

  • Select File > New > Other… and type in “Class”. This should make “Class” wizard visible on dialog box. Select “Class” and click “Next” button to view “New Java Class”
  • Type “HelloTomEEWs” as Name and click Finish.
  • Type in the following code and save the java class HelloTomEEWs. HelloTomEEWs is the interface for the web service that we are creating.
import javax.jws.WebService;

@WebService(targetNamespace = "http://sortedset.com/wsdl")
public interface HelloTomEEWs {

    public int sum(int add1, int add2);

    public int multiply(int mul1, int mul2);
}
  • Create another class named “HelloTomEEIMplementation . HelloTomEEIMplementation is the implementation web services. Type in code as below.
import javax.ejb.Stateless;
import javax.jws.WebService;

@Stateless
@WebService(
        portName = "HelloTomEEPort",
        serviceName = "HelloTomEEService",
        targetNamespace = "http://sortedset.com/wsdl",
        endpointInterface = "HelloTomEEWs")
public class HelloTomEEIMplementation implements HelloTomEEWs {

    public int sum(int add1, int add2) {
        return add1 + add2;
    }

    public int multiply(int mul1, int mul2) {
        return mul1 * mul2;
    }
}

Deploy WAR

  • Now select menu item File > Export… > Web > War file
  • Click Next button
  • Select “WebServicesExperiment” project from “Web project: drop down menu if it is no already selected.
  • On Destination: input box click “Browse” button.
  • Navigate to <TomEE_Home>\webapps and click “Save” button. This action should have “WebServicesExperiment.war” with full folder path location on Destination input box.
  • Select “TomEE Runtime” from Target runtime drop down box right under the check box named “Optimize for a specific server runtime. The “Expoert” window should look like below.

08

  • Click “Finish” button. This action should produce a WAR  under <TomEE_Home>\webapps folder.
  • Click windows Start > All Programs > Accessories > Command Prompt to bring up a Command Console
  • Change Directory CD to <TomEE_Home>\bin folder location.
  • For example TomEE installation location on the machine that was used for this blog was

S:\servers\apache-tomee-plus-1.5.1\

Run TomEE

  • Set JAVA_HOME environment variable to <Java_Home>.
  • Where jdk has been installed in S:\JDKs\64bit\jdk1.7.0_05 on the machine that was used to create this blog example.
  • The following command has been used to set Java_Home.

SET JAVA_HOME=S:\JDKs\64bit\jdk1.7.0_05

  • Start TomEE by typing startup in the command prompt. This action will bring up another command prompt with a title “Tomcat”. After spitting out a lot of system log the windows should stabilized with a line like “INFO: Server startup in 2560 ms” or something similar

09

View WSDL

  • Fire up a Firefox browser.
  • Type in the following url to view the WSDL of the web services that is running off of TomEE server

http://localhost:8080/WebServicesExperiment/webservices/HelloTomEEIMplementation?wsdl

  • You should see WSDL on Firefox

10

  • Note: It took a little bit time to find the WSDL url. Apache TomEE has a “Admin GUI” window. The url to WSDL was found logging in to TomEE Adming GUI and clicking on Console button. The consol button brought up a dialog box where the details of the web services were displayed.

Testing with soapUI

  • Download and install SoapUI from http://sourceforge.net/projects/soapui/files/
  • Start SoapUI
  • Select menu item File > New soapUI Project to bring up “New soapUI Project” dialog box.
  • Type in TomEE SoapUI Test in “Project Name” input box
  • Paste WSDL url on “Initial WSDL/WADL:” input box. For example for this blog example the wsdl is

http://localhost:8080/WebServicesExperiment/webservices/HelloTomEEIMplementation?wsdl

  • Click OK button

11

  • Double Click “Request 1” under multiply

12

  • Type in 4 between <arg0> and </arg0>
  • Type in 6 between <arg1> and </arg1>
  • Click Green Arrow button on top left to POST soap request to TomEE web service.

13

  • Server returns a soap message back with result 24

14

Testing With Firefox SOA Extension “Poser”

15

  • A soap message should be returned from the server with a result of 24 as shown below.

16

Test with Java Web Services Client

  • Open up Eclipse if it is not running already.
  • Create File > New > Java Project
  • Type TomEEClient on Project name: input box
  • Click “Finish”
  • Eclipse will ask for your permission to change perspective to “Java”. Select Yes.
  • On “Project Explorer” pane on the left right click on project “TomEEClient” and select “Properties” from the bottom of the dialog box.
  • Select “Java Build Path” from left pane.
  • Click on “Libraries” tab from right pane.
  • Then click on “Add Library…” button from right.

17

  • “Add Library” dialog box shows up.
  • Select “Server Runtime” from left pane and click “Next” button

18

  • Select  already configured from previous project “TomEE Runtime” and click “Finish” button

19

  • Click “OK” button to get out of “Properties for TomEEClient” window.
  • Right click on “src” from left pane and select “Class” wizard.

20

import javax.ejb.embeddable.EJBContainer;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;


public class TomEECalculatorClient {

	public static void main (String[] args) throws Exception{
		Properties properties = new Properties();
        properties.setProperty("openejb.embedded.remotable", "true");

        Service calculatorService = Service.create(
                new URL("http://localhost:8080/WebServicesExperiment/webservices/HelloTomEEIMplementation?wsdl"),
                new QName("http://sortedset.com/wsdl", "HelloTomEEService"));
        HelloTomEEWs calculator = calculatorService.getPort(HelloTomEEWs.class);
        
        System.out.println("Sum of 10 and 10 is : " + calculator.sum(10, 10));
        System.out.println("Multiple of 10 and 10 is : " + calculator.multiply(10, 10));
	}
	
}
  • Two Java code that was written as service interface and implementation for the previous web services project will need to be copied over to this client code as well. To make things simple this blog example is not using wsimport to generate Java artifacts from WSDL url as usually done as standard practice.
  • The Package Explorer pane should look like bellow with all three Java classes.

21

  • Now right click on “TomEECalculatorClient.java” and select “Run As” > “Java Application”
  • Java client should get results back from the server and the Console window of Eclipse should display result as below.

22

Some Notes

  • TomEE container is configured and packaged with Apache CXF runtime and required libraries. TomEE relieves you from configuration a web component only container like Tomcat with Apache CXF. That is the main selling point of TomEE that it enables you to write fully loaded JEE application server applications.
  • Selecting TomEE as runtime from within Eclipse also relieves a developer from using build tools like maven. Maven is a wonderful tool and it should be used a lot. However, sometime maven is tough to use specially if a person is developing behind a very strict company firewall where even a proxy is not allowed. In this kind of situation manual configuration is more desired.