1. IntroductionThis document will outline the process of developing a JAX-WS web service and deploying it using MyEclipse to the internal MyEclipse Tomcat server. The web service used in this tutorial will be a very simple calculator service that provides add, subtract, multiply and divide operations to the caller.MyEclipse also supports developing web services using the existing XFire framework from previous MyEclipse releases. For folks needing to develop and deploy WebSphere JAX-RPC or WebSphere JAX-WS web services, please take a look at our MyEclipse Blue Edition of the MyEclipse IDE. Additional resources covering web service creation using JAX-RPC, JAX-WS or XFire are included in the Resources section of this document. |
||||
2. System RequirementsThis tutorial was created with MyEclipse. However, if you notice portions of this tutorial looking different than the screens you are seeing, please let us know and we will make sure to resolve any inconsistencies. |
||||
3. Creating a ProjectTo get started we will create a simple Web Service Project by selecting Web Service Project from the new toolbar menu:Note: A JAX-WS web service can also be generated in any existing Java EE 5 web project. Name the project WebServiceProject. Note that JAX-WS support is only available for Java EE 5 projects and later, if you need to use an older project type (J2EE 1.4 or 1.3) you must use the XFire Web Services support instead: Now that we have created the web project, we can now create the simple Java class that we will use as the basis of our web service. |
||||
4. Creating the Service ClassThe service class is nothing more than a plain Java class that will provide implementations for the methods we want to expose as web service. In this particular tutorial we are going to write a simple 19-line Calculator class that implements a few typical operations found on a calculator.Let's first create a package for our new class under our Source directory by right-clicking on the Source directory and selecting New > Package: The package name we are going to use for this tutorial is com.myeclipseide.ws: Now we are going to create a new class, Calculator, in the new package we just created: As mentioned above, this simple class is a calculator implementation that provides the functions:
|
||||
5. Creating a Web ServiceNow that we have our service class written ( Calculator.java) we need to create a web service that exposes that server class as a web service. To do that we start by clicking the New Web Service toolbar button:On the next screen in the Strategy section, select the Bottom-up scenario since we have our Calculator class already and want to generate a JAX-WS web service from it: This is the last screen of the web service wizard. On this screen you have to select the Java bean that implements the operations for your web service, in our case it will be the single Calculator class that we wrote in the previous section: Once you set the Java bean class, MyEclipse will automatically fill out the remainder of the wizard fields for you. Of course you are welcome to customize or adjust them if you want, but in this case we are going to leave them all the way they are. Select Generate WSDL in project and hit Finish. After clicking Finish MyEclipse will generate the JAX-WS delegate class as well as the necessary JAX-WS descriptor into the WEB-INF directory of your web project. MyEclipse will also modify the web.xml file to include the new web service mappings so you can deploy and use the web service. Checking your project contents will show you all the artifacts that have been generated for you in order for that web service to be deployable to the target server: Now that our web service is created, we are ready to deploy it and test it out. |
||||
6. Deploying & Testing the Web ServiceJAX-WS is part of the Java EE 5 specification, if you are deploying your project to a full Java EE 5-compliant server, you should be ready to deploy your project right now without any further changes to the libraries. However, there are some common servers, like Jetty or Tomcat, that do not implement the entire Java EE 5 spec and will need the JAX-WS libraries deployed with your project in order to run.IMPORTANT: In MyEclipse we have tried to make this process easier by including the JAX-WS RI (Metro 1.1) with our embedded MyEclipse Tomcat server, so your web services will run out of the box on MyEclipse Tomcat, but if you plan to deploy to an external server that does not provide the JAX-WS libraries, you will need to follow the steps below on how to augment your build path to include them (so they are deployed). You can also install the JAX-WS libraries directly into your application server's /lib directory ususally if you don't want to add the libraries directly to your project; please check your Application Server's documentation for more information regarding that. If you do want to add the libraries to your project build path, please keep reading section 6.1, otherwise skip to section 6.2.
First thing we need to do is open our project's
Java Build Path >
Libraries preference page by right-clicking on our
project, and going to
Properties, from there we want to click the
Libraries button:
|
||||
7. Creating a Client for the Web ServiceNow that we have deployed our web service and tested it, we will use MyEclipse to generate a web service client for us. The web service client will allow us to interact directly with the web service and all it's exposed operations without needing to write all the marshalling or connection code ourselves.The first step to create the web service client will be to create a new Java Project to put the code into. We will do that by clicking the new toolbar button, then clicking Java Project: Now give your project a name and click Finish: After the project has been created, we now want to create a new Web Service Client. Select New Web Service Client from the web service toolbar menu: Now we select the project we want to generate the client into, and the type of client we want. In this case we are putting the client into the WebServiceClientProject we created and want it to be a JAX-WS client: The last step of the web service client creation is to specify either a WSDL File or a WSDL URL for the wizard to retrieve the web service WSDL from. In our case we are using the URL and generate the client into the new package com.myeclipseide.ws.client: http://localhost:8080/WebServiceProject/CalculatorPort?WSDL Now MyEclipse will load the WSDL for the web service you are attempting to create a client for and validate it for you, letting you know of any problems that may exist with the WSDL: NOTE: If any error occurs with validation, make sure the web service is deployed and the application server hosting it is running. If you are trying to generate a client to a 3rd party web service and get errors during the validation process, please bring it to the attention of the author of the web service if possible so it can be corrected. Now you can click Finish to have the client generated for you. After the client has been generated for you, you'll notice a new package in your Source folder as well as a few new classes that you can use to work with your web service: With the new generated resources we can utilize the CalculatorDelegate class to access a reference to our web service and then execute the operations that we exposed for it ( add, subtract, multiply and divide). In this tutorial let's write some code that uses the different operations from our web service. As an example let's say we want to compute the following 4 things:
After our class is generated for us, we need to provide an implementation of the main method so that it does the 4 mathematical calculations we listed above. The code, for the entire main method, to do those calculations with our web service and then print the results to the console is as follows:
When we do that, our client code runs, accesses our web service using the locator class generated for it and then we get the following output in our console:
(Source: Internet) |
Jun 24, 2013
The best tutorial - Developing JAX-WS Web Services & Clients
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment