...
The Web services deployed in the server uses CalculatorService interface as service end point interface. The important thing in this interface is it extends java.rmi.Remote interface and throws java.rmi.RemoteException from exposed methods.
Code Block |
---|
| java |
---|
| java |
---|
borderStyle | solid |
---|
title | CalculatorService.javajava |
---|
|
public interface CalculatorService extends java.rmi.Remote
{
public int addition( int x,int y ) throws java.rmi.RemoteException;
}
|
The service implementation class for the web service is CalculatorServiceServlet. It implements all the methods in the service end point interface. This class will be exposed as a servlet in the web.xml file eventhough it is not necessary to extend javax.servlet.Servlet class.
Code Block |
---|
| java |
---|
| java |
---|
borderStyle | solid |
---|
title | CalculatorServiceServlet.javajava |
---|
|
public class CalculatorServiceServlet {
public int addition(int x, int y){
return x + y;
}
}
|
Code Block |
---|
| xml |
---|
| xml |
---|
borderStyle | solid |
---|
title | web.xmlxml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:calc="urn:geronimo-samples"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<display-name>CalculatorServiceServlet</display-name>
<servlet-name>CalculatorServiceServlet</servlet-name>
<servlet-class>
org.apache.geronimo.samples.calc.web.CalculatorServiceServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CalculatorServiceServlet</servlet-name>
<url-pattern>/calculator</url-pattern>
</servlet-mapping>
</web-app>
|
Note the dependencies section of the geronimo-web.xml file. Those dependencies are mandotory to deploy web services in your web archive.
Code Block |
---|
| xml |
---|
| xml |
---|
borderStyle | solid |
---|
title | geronimo-web.xmlxml |
---|
|
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1" xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.1">
<dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.1">
<dep:moduleId>
<dep:groupId>org.apache.geronimo.samples</dep:groupId>
<dep:artifactId>Calculator</dep:artifactId>
<dep:version>1.0</dep:version>
<dep:type>car</dep:type>
</dep:moduleId>
<dep:dependencies>
<dep:dependency>
<dep:groupId>geronimo</dep:groupId>
<dep:artifactId>geronimo-webservices</dep:artifactId>
<dep:type>jar</dep:type>
</dep:dependency>
<dep:dependency>
<dep:groupId>geronimo</dep:groupId>
<dep:artifactId>geronimo-axis</dep:artifactId>
<dep:type>jar</dep:type>
</dep:dependency>
</dep:dependencies>
<dep:hidden-classes/>
<dep:non-overridable-classes/>
</dep:environment>
<context-root>/Calculator</context-root>
</web-app>
|
...
WSDL file describes about the web service as given in the below :
Code Block |
---|
| xml |
---|
| xml |
---|
borderStyle | solid |
---|
title | CalculatorServiceServlet.wsdlxml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="CalculatorServiceServlet" targetNamespace="urn:geronimo-samples" xmlns:tns="urn:geronimo-samples"
xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
<types/>
<message name="CalculatorService_addition">
<part name="int_1" type="xsd:int"/>
<part name="int_2" type="xsd:int"/>
</message>
<message name="CalculatorService_additionResponse">
<part name="result" type="xsd:int"/>
</message>
<portType name="CalculatorService">
<operation name="addition" parameterOrder="int_1 int_2">
<input message="tns:CalculatorService_addition"/>
<output message="tns:CalculatorService_additionResponse"/>
</operation>
</portType>
<binding name="CalculatorServiceBinding" type="tns:CalculatorService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="addition">
<soap:operation soapAction=""/>
<input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded"
namespace="urn:geronimo-samples"/>
</input>
<output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded"
namespace="urn:geronimo-samples"/>
</output>
</operation>
</binding>
<service name="CalculatorServiceServlet">
<port name="CalculatorServicePort" binding="tns:CalculatorServiceBinding">
<soap:address location="REPLACE_WITH_ACTUAL_URL"/>
</port>
</service>
</definitions>
|
jaxrpc-mapping file will map the information with service implementation.
Code Block |
---|
| xml |
---|
| xml |
---|
borderStyle | solid |
---|
title | jaxrpc-mapping.xmlxml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<java-wsdl-mapping xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.1" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://www.ibm.com/webservices/xsd/j2ee_jaxrpc_mapping_1_1.xsd">
<package-mapping>
<package-type>org.apache.geronimo.samples.calc</package-type>
<namespaceURI>urn:geronimo-samples</namespaceURI>
</package-mapping>
<package-mapping>
<package-type>org.apache.geronimo.samples.calc</package-type>
<namespaceURI>urn:geronimo-samples</namespaceURI>
</package-mapping>
<service-interface-mapping>
<service-interface>
org.apache.geronimo.samples.calc.CalculatorService
</service-interface>
<wsdl-service-name xmlns:serviceNS="urn:geronimo-samples">serviceNS:CalculatorServiceServlet</wsdl-service-name>
<port-mapping>
<port-name>CalculatorServicePort</port-name>
<java-port-name>CalculatorServicePort</java-port-name>
</port-mapping>
</service-interface-mapping>
<service-endpoint-interface-mapping>
<service-endpoint-interface>org.apache.geronimo.samples.calc.CalculatorService</service-endpoint-interface>
<wsdl-port-type xmlns:portTypeNS="urn:geronimo-samples">portTypeNS:CalculatorService</wsdl-port-type>
<wsdl-binding xmlns:bindingNS="urn:geronimo-samples">bindingNS:CalculatorServiceBinding</wsdl-binding>
<service-endpoint-method-mapping>
<java-method-name>addition</java-method-name>
<wsdl-operation>addition</wsdl-operation>
<method-param-parts-mapping>
<param-position>0</param-position>
<param-type>int</param-type>
<wsdl-message-mapping>
<wsdl-message xmlns:wsdlMsgNS="urn:geronimo-samples">wsdlMsgNS:CalculatorService_addition</wsdl-message>
<wsdl-message-part-name>int_1</wsdl-message-part-name>
<parameter-mode>IN</parameter-mode>
</wsdl-message-mapping>
</method-param-parts-mapping>
<method-param-parts-mapping>
<param-position>1</param-position>
<param-type>int</param-type>
<wsdl-message-mapping>
<wsdl-message xmlns:wsdlMsgNS="urn:geronimo-samples">wsdlMsgNS:CalculatorService_addition</wsdl-message>
<wsdl-message-part-name>int_2</wsdl-message-part-name>
<parameter-mode>IN</parameter-mode>
</wsdl-message-mapping>
</method-param-parts-mapping>
<wsdl-return-value-mapping>
<method-return-value>int</method-return-value>
<wsdl-message xmlns:wsdlMsgNS="urn:geronimo-samples">wsdlMsgNS:CalculatorService_additionResponse</wsdl-message>
<wsdl-message-part-name>result</wsdl-message-part-name>
</wsdl-return-value-mapping>
</service-endpoint-method-mapping>
</service-endpoint-interface-mapping>
</java-wsdl-mapping>
|
webservices.xml file gives the locations of WSDL and jaxrpc-mapping files to services. The developer needs to have a good understanding of this file than the others given above.
Code Block |
---|
| xml |
---|
| xml |
---|
borderStyle | solid |
---|
title | webservices.xmlxml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<webservices xmlns="http://java.sun.com/xml/ns/j2ee" version="1.1">
<webservice-description>
<webservice-description-name>
CalculatorServiceServlet
</webservice-description-name>
<wsdl-file>WEB-INF/wsdl/CalculatorServiceServlet.wsdl</wsdl-file>
<jaxrpc-mapping-file>WEB-INF/jaxrpc-mapping.xml</jaxrpc-mapping-file>
<port-component>
<port-component-name>CalculatorServicePort</port-component-name>
<wsdl-port>CalculatorServicePort</wsdl-port>
<service-endpoint-interface>
org.apache.geronimo.samples.calc.CalculatorService
</service-endpoint-interface>
<service-impl-bean>
<servlet-link>CalculatorServiceServlet</servlet-link>
</service-impl-bean>
</port-component>
</webservice-description>
</webservices>
|
How to access the Web service deployed in the server in tool independent manner? CalculatorClient.java file demonstrates how to achieve this function.
Code Block |
---|
| java |
---|
| java |
---|
borderStyle | solid |
---|
title | CalculatorClient.javajava |
---|
|
String urlstr = "http://localhost:8080/Calculator/calculator?wsdl";
URL url = new URL(urlstr);
QName qname = new QName("urn:geronimo-samples","CalculatorServiceServlet");
ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(url, qname);
CalculatorService calc = (CalculatorService)service.getPort(CalculatorService.class);
int sum = calc.addition(x, y);
System.out.println("Sum of "+x+" and "+y+" is "+sum);
|
...