JAX-WS Development With Eclipse - 6.3

Talend ESB Service Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
Installation and Upgrade
EnrichPlatform
Talend ESB

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:

  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), select the Server -> Runtime Environments section and add your Tomcat installation to the IDE:

    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 Eclipse Preferences, open up the Web Services -> CXF 2.x Preferences section and enter the CXF directory as shown below.

    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.

    We 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 press the Finish button as illustrated:

  4. Create the web service provider.

    We'll start from a WSDL-first approach.

    1. First we'll 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 (i.e., WSDL-first) web service approach 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.

      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 hardcoded number, we'll need to modify the web service provider to return the desired result, which we'll do next.

    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.

    We'll now use Eclipse to create a Java-based web service client. Right-click WebServiceSample from the Project Explorer and select New -> Other, and from the New dialog 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 select "Next" and enter another package name for the client (say "org.example.doubleit.client") so it does not conflict with the web service provider package. Click Finish.

    Two modifications to the autogenerated 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 wish to double (say "55") and then 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