Create SOAP Web Service in Java using Eclipse

In this tutorial, we will build a web service using SOAP.

Step 1 : Create a dynamic web project in eclipse/STS

Step 2 : Fill in the deails as below

Step 3 : Click Next. Don’t forget to click the check boxes as shown below

Step 4 : Click Finish. The dynamic web project should be created like below

Step 5 : Create a Model Class called Operand under com.sks package

package com.sks; public class Operand < private int num1; private int num2; public int getNum1() < return num1; >public void setNum1(int num1) < this.num1 = num1; >public int getNum2() < return num2; >public void setNum2(int num2) < this.num2 = num2; >@Override public String toString() < return "Operand [num1=" + num1 + ", num2=" + num2 + "]"; >>

Step 6 : Create an interface CalculationService. It will have operations like sum, multiplication

package com.sks; public interface CalculationService 

Step 7 : Create its implementation class CalculationServiceImpl. It implements CalculationService interface

package com.sks; public class CalculationServiceImpl implements CalculationService < @Override public int sum(Operand op) < return op.getNum1() + op.getNum2(); >@Override public int multiplication(Operand op) < return op.getNum1() * op.getNum2(); >> 

Now, our dynamic web project is ready with some logic. Next step is to create a web service from it.

Step 8 : Create a new project in STS/eclipse and choose ‘Web Service’ under ‘Web Services’ option as shown below

Step 9 : Now, click on Next and verify the details on the next screen. Make sure Web Service Type is selected as Bottom Up Java Bean Web Service.

Also, select Service implementation as our class CalculationServiceImpl. Also drag the Configuration bar to the maximum on the left hand side. It should be upto ‘Test Service’.

Step 10 : On the next screen, we can select which all methods we want to expose as web service. For this example, we will choose both

Step 11 : Click on Next. And cick on Start server button. Once it is started, click on Next button.

Step 12 : Below screen will appear. Test facility is selected as Web Services Explorer. Click on the Launch button.

Step 12 : Web Services Explorer will open in a new window. It will look like below. Don’t do anything here and move to the next step.

Step 13 : On the Eclipse, click on the Next button in the below screen

Step 14 : The below screen will appear. You don’t have to do anything on this screen. Just click on finish.

Step 15 : Go back to Web Services Explorer screen and click on sum as highlighted

Step 16 : Give values in the num1 and num2 boxes and click Go button

Step 17 : The result 26 can be seen under Status

Step 18 : To check the soap generated request and response, click on the source as shown below

SOAP request

SOAP request for this method is as below

    12 14    

SOAP response

WSDL File

The WSDL file can be seen under the webapp folder as shown below

Below is the content of wsdl file