AXIS 2 Sample web service with maven and eclipse
-
Create meven project in eclipse
-
Create Calculator Service Class
- Create new package name as com.ruks.services
- Create Caculator.java class inside the package.
- Copy the below code to Caculator.java
package com.ruks.serivces;
public class Calculator {
private static Calculator cal;
private int X=1;
private int Y=2;
private int Result;
private Calculator(){
}
public static Calculator getCal(){
if(cal==null){
cal=new Calculator();
}
return cal;
}
public void setX(int x){
this.X=x;
// System.out.println("set x: "+CalculatorService.X+" = "+x);
}
public void setY(int y){
this.Y=y;
// System.out.println("set y: "+CalculatorService.Y+" = "+y);
}
public int getSum(){
this.Result=this.X+this.Y;
// System.out.println("GET result: "+this.Result);
return this.Result;
}
}
- Create CaculatorService.java class inside the package.
- Copy the code below to CaculatorService.java
package com.ruks.serivces;
public class CalculatorService {
Calculator cal;
public CalculatorService() {
cal=Calculator.getCal();
}
public void setX(int x){
cal.setX(x);
// System.out.println("set x: "+CalculatorService.X+" = "+x);
}
public void setY(int y){
cal.setY(y);
// System.out.println("set y: "+CalculatorService.Y+" = "+y);
}
public int getSum(){
return cal.getSum();
}
}
-
Create service.xml file
- Add a xml file to main/resources as service.xml
- Add the this to the service.xml
<?xml version="1.0" encoding="UTF-8"?>
<service name="CalculatorService">
<description>
Simple Calculator Implemantain
</description>
<parameter name="ServiceClass">com.ruks.serivces.CalculatorService</parameter>
<operation name="setX" mep="http://www.w3.org/ns/wsdl/in-out" namespace="http://serivces.ruks.com/xsd">
<actionMapping>http://serivces.ruks.com/xsd/setX</actionMapping>;
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />;
</operation>;
<operation name="setY" mep="http://www.w3.org/ns/wsdl/in-out" namespace="http://serivces.ruks.com/xsd">;
<actionMapping>;http://serivces.ruks.com/xsd/setY</actionMapping>;
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />;
</operation>;
<operation name="getSum" mep="http://www.w3.org/ns/wsdl/in-out" namespace="http://serivces.ruks.com/xsd">;
<actionMapping>;http://serivces.ruks.com/xsd/getSum</actionMapping>;
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />;
</operation>;
</service>;
-
Edit the pom.xml
- add the axis2-java2wsdl-maven-plugin
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-java2wsdl-maven-plugin</artifactId>
<configuration>
<className>com.ruks.services.CalculatorService</className>
<outputFileName>${project.build.directory}/CalculatorService.wsdl</outputFileName>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>java2wsdl</goal>
</goals>
</execution>
</executions>
</plugin>
- add the axis2-aar-maven-plugin
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-aar-maven-plugin</artifactId>
<configuration>
<servicesXmlFile>${basedir}/src/resources/services.xml</servicesXmlFile>
<wsdlFile>${project.build.directory}/CalculatorService.wsdl</wsdlFile>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>aar</goal>
</goals>
</execution>
</executions>
</plugin>
-
Build the achive
- Execute maven goal clean package to create aar achive
- appropriate wsdl file and aar file will be created in the target directory
-
Deploy the web service
- Copy the Caculator.aar and paste to apache-tomcat/webapps/axis2/WEB-INF/services
- Start the tomcat
-
Verify the Service
- Start the SoapUI
- Navigate to http://localhost:8080/axis2/services/listServices
- Make sure CalculatorService is listed
- Click the CalculatorService and you get the WSDL URL
- copy WSDL URL
- Create new SoapUI soap Project
- Add the URL to initial WSDL text box
- Select the Soap Binding
- Set the SetX request as below
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.ruks.com">
<soapenv:Header/>
<soapenv:Body>
<ser:setX>
<!--Optional:-->
<ser:x>10</ser:x>
</ser:setX>
</soapenv:Body>
</soapenv:Envelope>
- Set the SetY request as below
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.ruks.com">
<soapenv:Header/>
<soapenv:Body>
<ser:setY>
<!--Optional:-->
<ser:y>20</ser:y>
</ser:setY>
</soapenv:Body>
</soapenv:Envelope>
- Run the getSum request to see output
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:getSumResponse xmlns:ns="http://services.ruks.com">
<ns:return>30</ns:return>
</ns:getSumResponse>
</soapenv:Body>
</soapenv:Envelope>
Add Comment
Comments (0)