Creating a Web Service
The starting point for developing a web service to use the WSIT technologies is a Java class file annotated with the
javax.jws.WebService
annotation. TheWebService
annotation defines the class as a web service endpoint. The following Java code shows a web service. The IDE will create most of this Java code for you.package org.me.calculator; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam;@WebService()
public class Calculator {@WebMethod(action="sample_operation")
public String operation(@WebParam(name="param_name") String param) { // implement the web service operation here return param; } @WebMethod(action="add") public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) { int k = i + j; return k; } }Notice that this web service performs a very simple operation. It takes two integers, adds them, and returns the result.
Perform the following steps to use the IDE to create this web service:
- Click on the Runtime tab in the left pane and verify that GlassFish is listed in the left pane. If it is not listed, refer to Registering GlassFish with the IDE and register it.
- Choose File
New Project, select Web Application from the Web category, and click Next.
- Assign the project a name that is representative of services that will be provided by the web service (for example, CalculatorApplication), set the Project Location to the location of the Sun application server, and click Finish.
Note: As of this writing, when creating the web service project be sure to define a Project Location that does not include spaces in the directory name. Spaces in the directory might cause the web service and web service clients to fail to build and deploy properly. To avoid this problem, Sun recommends that you create a directory, for example
C:\work
, and put your project there.
- Right-click the CalculatorApplication node and choose New
Web Service.
- Enter the web service name (
CalculatorWS
) and the package name (org.me.calculator
) in the Web Service Name and the Package fields respectively.- Select Create an Empty Web Service.
- Click Finish.
The IDE then creates a skeleton
CalculatorWS.java
file for the web service that includes an emptyWebService
class with annotation@Webservice
.- Right-click within the body of the class and choose Web Service
Add Operation.
- In the upper part of the Add Operation dialog box, type
add
in Name and chooseint
from the Return Type drop-down list.- In the lower part of the Add Operation dialog box, click Add and create a parameter of type
int
namedi
. Click OK. Click Add again and create a parameter of typeint
calledj
. Click OK and close the Enter Method Parameter dialog box.- Click OK at the bottom of the Add Operation dialog box.
- Notice that the
add
method has been added to the Source Editor:
@WebMethod
public int add(@WebParam(name = "i") int i,
@WebParam(name = "j") int j) {
// TODO implement operation
return 0;
}- Change the add method to the following (changes are in bold):
@WebMethod
(action="add")
public int add(@WebParam(name = "i") int i,
@WebParam(name = "j") int j) {
int k = i + j;
return
k
;
}
Note: To ensure interoperability with Windows Communication Foundation (WCF) clients, you must specify the
action
element of@WebMethod
in your endpoint implementation classes. WCF clients will incorrectly generate an empty string for the Action header if you do not specify theaction
element.
You have successfully coded the web service.