Example of SOAP RPC Web Service in Java

In this article, we will show how to create a simple RPC web service example in java using SOAP and associated with its client in a servlet container. This example is used further in Secure Web Service with Basic Authentication and Secure SOAP Web Service over SSL.

1. Create a Web Service Endpoint Interface

Let's start with creating SEI with two methods:

package example;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface ExamClouds {
    @WebMethod
    String getSiteName();

    @WebMethod
    String getSiteDescription();
}

2. Create a Web Service Endpoint Implementation

package example;

import javax.jws.WebService;

@WebService(endpointInterface = "example.ExamClouds")
public class ExamCloudsImpl implements ExamClouds {
    public String getSiteName() {
        return "http://www.examclouds.com";
    }

    public String getSiteDescription() {
        return "Java Tutorial";
    }
} 
                

3. Create web.xml

<servlet>
    <servlet-name>ExamClouds</servlet-name>
    <servlet-class>example.ExamCloudsImpl</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>ExamClouds</servlet-name>
    <url-pattern>/ExamClouds</url-pattern>
</servlet-mapping>
                               

4. Deploy Web Service

Compile and package the web service into a WAR file, and deploy it into a Java EE servlet container.

5. Test a Web Service

The deployed web service can be tested by accessing the generated WSDL document via the URL “http://<YOUR_HOST>/ws1-1.0-SNAPSHOT/ExamClouds?wsdl”.

6. Create a Web Service Client

The wsimport tool helps with creating soap web service client example in java:

wsimport -s src/main/java -p example.client -keep
http://<YOUR_HOST>/ws1-1.0-SNAPSHOT/ExamClouds?wsdl
                     

7. Run the Client

And finally let's call the web service:

ExamCloudsImplService service = new ExamCloudsImplService();
ExamClouds port = service.getExamCloudsImplPort();
System.out.println(port.getSiteName());
System.out.println(port.getSiteDescription());
                      

 

Read also: Лучшие онлайн курсы JavaТесты по Java с ответамиJava вопросы на собеседовании.

Trustpilot
Trustpilot
Comments