In this simple tutorial I will demonstrate step-by-step how to build a java web service using Java API for XML Web Services JAX-WS in Eclipse. The JAX-WS API is available in Java SE 1.5 and later. JAX-WS can be used to build web services and web service clients that communicate using XML messages. For the purpose of this tutorial I will be using:
Now we will use "wsgen" web service generation tool which will generate the web service portable artifacts. Start a terminal or command prompt and make sure JDK is in your PATH, then navigate to the root directory of the project and execute the wsgen command:
wsgen -verbose -keep -cp build/classes/ -wsdl -r WebContent/WEB-INF/ -s src/ -d build/classes/ com.moeabdol.Calculator
For more information about wsgen options, review the wsgen manual. If you're on a unix like machine just man wsgen.
Now, in Eclipse right-click the project and refresh it. You'll notice that wsgen has created some new source files and class files in your project. Most importantly, for each web method you created wsgen has created two java files for it. For example, the web method add which you have created wsgen created an add.java and an addResponse.java. These files will handle the requests and responses to this web methods. Moreover, wsgen also created a WSDL file for the web service implemented at com.moeabdol.Calculator.
We are almost done here. All we have to do next is to modify the WSDL file by changing where it says "REPLACE_WITH_ACTUAL_URL" with the actual URL of the service "http://localhost:8089/SimpleCalculatorWebService/CalculatorService". Note that I'm deploying this web service to a locally installed instance of Glassfish and i configured Glassfish to use port 8089.
- Eclipse IDE (Indigo) to create the web service.
- Glassfish application server to host the web service.
First open Eclipse and create a "Dynamic Web Project". Name the project "SimpleCalculatorWebService". Set the Target Runtime to Glassfish, and the configuration to minimal.
Take note of your source and output folders. We will use them later to generate the web service artifacts. Click finish to allow Eclipse to generate the project.
Create a new class under the source folder. This will be the implementation class of the web service. I named the class "Calculator". The following code is the web service implementation in the Calculator class. Note that I have added specific JAX-WS annotations. @WebService to imply this class is an implementation of a web service, and a @WebMethod which implies that this is a web service method implementation.
package com.moeabdol; // jaxws imports import javax.jws.WebService; import javax.jws.WebMethod; @WebService public class Calculator { @WebMethod public double add(double x, double y){ return x + y; } @WebMethod public double subtract(double x, double y){ return x - y; } @WebMethod public double multiply(double x, double y){ return x * y; } @WebMethod public double divide(double x, double y){ if(y == 0) return 0; else return x / y; } }
Now we will use "wsgen" web service generation tool which will generate the web service portable artifacts. Start a terminal or command prompt and make sure JDK is in your PATH, then navigate to the root directory of the project and execute the wsgen command:
wsgen -verbose -keep -cp build/classes/ -wsdl -r WebContent/WEB-INF/ -s src/ -d build/classes/ com.moeabdol.Calculator
For more information about wsgen options, review the wsgen manual. If you're on a unix like machine just man wsgen.
Now, in Eclipse right-click the project and refresh it. You'll notice that wsgen has created some new source files and class files in your project. Most importantly, for each web method you created wsgen has created two java files for it. For example, the web method add which you have created wsgen created an add.java and an addResponse.java. These files will handle the requests and responses to this web methods. Moreover, wsgen also created a WSDL file for the web service implemented at com.moeabdol.Calculator.
We are almost done here. All we have to do next is to modify the WSDL file by changing where it says "REPLACE_WITH_ACTUAL_URL" with the actual URL of the service "http://localhost:8089/SimpleCalculatorWebService/CalculatorService". Note that I'm deploying this web service to a locally installed instance of Glassfish and i configured Glassfish to use port 8089.
Right-Click the project and select Run -> Run On Server select Glassfish and click finish. The web service will be deployed on Glassfish. Now we don't have a web service client to test the web service, so we can use a facility provided by Eclipse. Under WEB-INF folder right-click on CalculatorService.wsdl and select Web Services -> Test with Web Services Explorer. Select the add method and enter any chosen numbers for arg0 and arg1 and click go. The response from the web service should show the result.
No comments :
Post a Comment