...
- Create a Dynamic Web Project
- Select File -> New, select Other...
- In the popup window select Web->Dynamic Web Project category and click NEXT.
- Type in jaxws-calculator as the Project name: and select Next
- On the next screen check the box for Axis2 Web Services
- Make sure Generate Deployment Descriptor is selected and click Next.
- Modify the Group Id to: org.apache.geronimo.samples.jws and Artifact Id: to Calculator.
Info title Useful Information Group ID: A name that identifies the group of a module. The default value is default.
Artifact ID: A name of the module in a group. The default value is <blank>.
Version: The version of module. The default value is 1.0
Type: The type of module, such as system module (car), jar module (jar), web
archive (war), enterprise archive (ear), and so on. The default value is car
A module is stored inside a repository as repository/groupId/artifactId/version/artifactIdversion.
type. This repository structure gives a more standardized and easily maintainable
structure to Geronimo applications. - Select Finish.
Warning title Warning Depending upon your environment, you may see the error message "Failed while installing Axis2 Web Services Core 1.1". Do not worry. We will just have to manually point Axis2 to the installation directory of Apache Geronimo to fix this error.
- Select File -> New, select Other...
- AXIS2 Configuration
- Select Window->Preferences.
- Select Web Services->Axis2 Preferences.
- Under Axis2 Runtime tab, browse to the installation root of Apache Geronimo. Select Ok.
This completes the configuration of Eclipse for application development.
- Select Window->Preferences.
Define the wsdl for the web service
Creating the Web Services Implementation code
To implement the Calculator we are going to create a package org.apache.geronimo.samples.jws. This package will contain 2 classes. A Calculator Interface, and CalculatorService which implements the Calculator interface. Lets go step by step with creating the package, interface class and implementation class.
- Right click on Java Resources: src and select New->Package.
- Name the package as org.apache.geronimo.samples.jws. Select Finish.
- Right click on the new package and Select New->Interface.
- Name the interface as Calculator. Select Finish.
- Add the following code to the Calculator interface class
Right Click on the WebContent/WEB-INF subfolder and Select New->Other.Code Block title Calculator.class borderStyle solid package org.apache.geronimo.samples.jws; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; @WebService(name="CalculatorPortType", targetNamespace =
Select Web Services->wsdl and click Next.
Give the File name as CalculatorService.wsdl and click Next.
On the next window give the Target namespace as "http://jws.samples.geronimo.apache.org". Select Finish.
Modify the CalculatorService.wsdl as follows
This completes the creation of wsdl for web service.Code Block title CalculatorService.wsdl borderStyle solid <wsdl:definitions name="Calculator" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://jws.samples.geronimo.apache.org" xmlns:tns="http://jws.samples.geronimo.apache.org"> <wsdl:types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://jws.samples.geronimo.apache.org" targetNamespace="http://jws.samples.geronimo.apache.org" attributeFormDefault="unqualified" elementFormDefault="qualified"> <xsd:element name="add"> <xsd:complexType> <xsd:sequence> <xsd:element name="value1" type="xsd:int"/> <xsd:element name="value2" type="xsd:int"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="addResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="return" type="xsd:int"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="add"> <wsdl:part name="add" element="tns:add"/> </wsdl:message> <wsdl:message name="addResponse"> <wsdl:part name="addResponse" element="tns:addResponse"/> </wsdl:message> <wsdl:portType name="CalculatorPortType"> <wsdl:operation name="add"> <wsdl:input name="add" message="tns:add"/> <wsdl:output name="addResponse" message="tns:addResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="CalculatorSoapBinding" type="tns:CalculatorPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="add"> <soap:operation soapAction="add" style="document"/> <wsdl:input name="add"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="addResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="Calculator"> <wsdl:port name="CalculatorPort" binding="tns:CalculatorSoapBinding"> <soap:address location="http://localhost:8080/jaxwscalculator- 1.0/calculator"/> <wswa:UsingAddressing xmlns:wswa="http://www.w3.org/2005/08/addressing/wsdl"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
Creating the Web Services Implementation code
To implement the Calculator we are going to create a package org.apache.geronimo.samples.jws. This package will contain 2 classes. A Calculator Interface, and CalculatorService which implements the Calculator interface. Lets go step by step with creating the package, interface class and implementation class.
...
Code Block | ||||
---|---|---|---|---|
| ||||
package org.apache.geronimo.samples.jws;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(name="CalculatorPortType",
targetNamespace = "http://jws.samples.geronimo.apache.org")
public interface Calculator {
@WebMethod
public int add(@WebParam(name = "value1") int value1,
@WebParam(name = "value2") int value2);
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
package org.apache.geronimo.samples.jws;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
@WebService(serviceName = "Calculator",
portName="CalculatorPort",
endpointInterface = "org.apache.geronimo.samples.jws.Calculator",
targetNamespace = "http://jws.samples.geronimo.apache.org",
wsdlLocation = "WEB-INF/CalculatorService.wsdl")
public class CalculatorService implements Calculator {
@Resource
private WebServiceContext context;
public int add(int value1, int value2) {
System.out.println("User Principal: " + context.getUserPrincipal());
return value1 + value2;
}
}
|
) public interface Calculator { @WebMethod public int add(@WebParam(name = "value1") int value1, @WebParam(name = "value2") int value2); }
- Right click on the package org.apache.geronimo.samples.jws and select
New->Class. - Name the class CalculatorService
- Accept all the defaults and Select Finish.
- Add the following code to CalculatorService.class
Code Block title CalculatorService.class borderStyle solid package org.apache.geronimo.samples.jws; import javax.annotation.Resource; import javax.jws.WebService; import javax.xml.ws.WebServiceContext; @WebService(serviceName = "Calculator", portName="CalculatorPort", endpointInterface = "org.apache.geronimo.samples.jws.Calculator", targetNamespace = "http://jws.samples.geronimo.apache.org", wsdlLocation = "WEB-INF/CalculatorService.wsdl") public class CalculatorService implements Calculator { @Resource private WebServiceContext context; public int add(int value1, int value2) { System.out.println("User Principal: " + context.getUserPrincipal()); return value1 + value2; } }
This completes the development of Web Services implementation code.
Generating the wsdl for the web service
- Right Click on the WebContent/WEB-INF subfolder and Select New->Other.
- Select Web Services->wsdl and click Next.
- Give the File name as CalculatorService.wsdl and click Next.
- On the next window give the Target namespace as "http://jws.samples.geronimo.apache.org". Select Finish.
- Modify the CalculatorService.wsdl as follows
This completes the creation of wsdl for web serviceCode Block title CalculatorService.wsdl borderStyle solid <wsdl:definitions name="Calculator" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://jws.samples.geronimo.apache.org" xmlns:tns="http://jws.samples.geronimo.apache.org"> <wsdl:types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://jws.samples.geronimo.apache.org" targetNamespace="http://jws.samples.geronimo.apache.org" attributeFormDefault="unqualified" elementFormDefault="qualified"> <xsd:element name="add"> <xsd:complexType> <xsd:sequence> <xsd:element name="value1" type="xsd:int"/> <xsd:element name="value2" type="xsd:int"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="addResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="return" type="xsd:int"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="add"> <wsdl:part name="add" element="tns:add"/> </wsdl:message> <wsdl:message name="addResponse"> <wsdl:part name="addResponse" element="tns:addResponse"/> </wsdl:message> <wsdl:portType name="CalculatorPortType"> <wsdl:operation name="add"> <wsdl:input name="add" message="tns:add"/> <wsdl:output name="addResponse" message="tns:addResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="CalculatorSoapBinding" type="tns:CalculatorPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="add"> <soap:operation soapAction="add" style="document"/> <wsdl:input name="add"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="addResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="Calculator"> <wsdl:port name="CalculatorPort" binding="tns:CalculatorSoapBinding"> <soap:address location="http://localhost:8080/jaxwscalculator- 1.0/calculator"/> <wswa:UsingAddressing xmlns:wswa="http://www.w3.org/2005/08/addressing/wsdl"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
...
- .
Developing a web client for Calculator
...