-
-
- Tips and Tricks
Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering
In this example, we create a SOAP based web service for a simple Java Calculator class with operations ‘add’ and ‘subtract’. We then create a web service client which consumes the web service and displays the result of the invoked web service.
package de.unibonn.se.atsc.ws.calc; import javax.jws.WebService; @WebService public class Calculator { public int add(int a, int b) { return (a + b); } public int sub(int a, int b) { return (a - b); } }
wsgen -s src -cp bin -d bin de.unibonn.se.atsc.ws.calc.Calculator
The –cp option specifies the classpath for our Calculator class which is in “bin” folder, the –d option specifies where to place generated output files which is also the ‘bin’ folder in our case and the “-s src” tells the generator to create the source class in the src-folder.
package de.unibonn.se.atsc.ws.calc; import javax.xml.ws.Endpoint; import de.unibonn.se.atsc.ws.calc.Calculator; public class CalcEndpointPublisher { public static void main(String[] args) { Endpoint.publish("http://localhost:8080/CalcWS/Calculator", new Calculator()); } }
http://localhost:8080/CalcWS/Calculator?wsdl
package de.unibonn.se.atsc.ws.calc.client; import de.unibonn.se.atsc.ws.calc.Calculator; import de.unibonn.se.atsc.ws.calc.CalculatorService; public class CalcClient { public static void main(String[] args) { int a = 10; int b = 12; CalculatorService calcService = new CalculatorService(); Calculator calc = calcService.getCalculatorPort(); System.out.println(a + " + " + b + " = " + calc.add(a, b)); System.out.println(a + " - " + b + " = " + calc.sub(a, b)); } }
10 + 12 = 22 10 – 12 = -2