Sunday, June 4, 2017

SOAP web services standalone application with CXF framework

This is a simple Java SOAP web services application using CXF framework.

1. Setup
Download CXF run-time binary at http://cxf.apache.org/download.html and add it to your Eclipse.




2. Create an interface CalcInterface.java which declares two web services add and multiply.

package math.util;
public interface CalcInterface {

     int add(int a, int b);

  int multiply(int a, int b);

}

3.       Create implementation class for this interface Calc.java

package math.util;

public class Calc implements CalcInterface {

   public int add(int a, int b) {
   return a + b;
   }

   public int multiply(int a, int b) {
          return a * b;
   }
}




4. Create WSDL file for the interface
Right click on CalcInterface.java and select Web services -> Create Web Service


 

Input the WSDL file name you want to create (calc.wsdl).



CXF needs startup web server to generate stuffs. Assuming you already created a web server.




Last, click Finish to generate web services stuffs.



You will get file calc.wsdl and server stuffs as below. All is for web service for server side.



By default, generated code will set web service endpoint at address:
Type below URL and start web service server (just run main method in file Calc_PortTypeServer.java) to see your services:

5. Create web services for Client side.
Right click on file calc.wsdl and select Web services -> Generate client.




You will get:

Go to CalcService.java and change URL of the service endpoint to http://localhost:9090/CalcPort?wsdl.


Finally, Go to file Calc_CalcPort_Client.java and run the main method to test your services. Remember to launch CXF server in class Calc_PortTypeServer.java first. Note: this is a standalone application so it doesn't request web server as Tomcat. Actually, CXF run-time already integrated a web server for its functions.



Or to change arguments and see the results



Source Code at github

No comments:

Post a Comment