# Create Java SOAP Web Service (RPC style)

In 
Published 2022-12-03

This tutorial explains to you how to create a simple Java Web Service using SOAP RPC style. This Java Web Service will not be deployed on a Java Application Server. The article below is just a JAX-WS RPC Hello World example.

In order to create a simple JAX-WS example using the RPC style, I will create a "Hello World" example. A Web Service can be deployed "standalone" (without an application server) and on an application server. In my case the JAX-WS using RPC style will be deployed without an application server. Creating a Web Service (RPC Style) is very simple.

I will create an interface:

package ws;
 
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
 
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorldServerInt {
 
    @WebMethod
    String sayHelloWorld(String name);
}

I will create the implementation of the interface:

package ws;
import javax.jws.WebService;
 
@WebService(endpointInterface = "ws.HelloWorldServerInt")
public class HelloWorldServerImpl 
                         implements HelloWorldServerInt {
 
    public String sayHelloWorld(String name) {
        return "Hello World ! My name is " + name + ".";
    }
}

I will create a "Publisher" class for publishing the web service:

package ws;
 
import javax.xml.ws.Endpoint;
 
public class HelloWorldServerPublisher {
 
    public static void main(String[] args) {
 
      System.out.println("Start publishing HelloWorld Web Service now ...");
      Endpoint.publish("http://localhost:8081/helloworld", new ws.HelloWorldServerImpl());
      System.out.println("Your Hello World JAX-WS RPC is published !");
    }
}

In order to run the JAX-WS, you have to compile it, create a jar file and execute it from the OS level. I have used "Apache Ant" tool for this purpose.

When I run the jar file I receive the following output into the console:

Start publishing HelloWorld Web Service now ...
Your Hello World JAX-WS RPC is published !

Here are some remarks:

  • The service endpoint interface (SEI) is a Java interface class that defines the methods to be exposed as a Web service.
  • @WebService annotation define this class as SEI
  • @SOAPBinding add information about the form of the JAX-WS messages:
  • @WebMethod : this method can be called through the Web.

  • You can deploy the Web Service on an Application Server. In this case you don't need a Publisher Class.

When you publish the web service, you can see the WSDL at the following address :

http://localhost:8081/helloworld?wsdl

Here is the content of the WSDL file:

<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws/" name="HelloWorldServerImplService">
  <types/>
  <message name="sayHelloWorld">
    <part name="arg0" type="xsd:string"/>
  </message>
  <message name="sayHelloWorldResponse">
    <part name="return" type="xsd:string"/>
  </message>
  <portType name="HelloWorldServerInt">
    <operation name="sayHelloWorld">
        <input wsam:Action="http://ws/HelloWorldServerInt/sayHelloWorldRequest" message="tns:sayHelloWorld"/>
        <output wsam:Action="http://ws/HelloWorldServerInt/sayHelloWorldResponse" message="tns:sayHelloWorldResponse"/>
    </operation>
  </portType>
  <binding name="HelloWorldServerImplPortBinding" type="tns:HelloWorldServerInt">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
  <operation name="sayHelloWorld">
  <soap:operation soapAction=""/>
  <input>
    <soap:body use="literal" namespace="http://ws/"/>
  </input>
  <output>
    <soap:body use="literal" namespace="http://ws/"/>
  </output>
  </operation>
  </binding>
  <service name="HelloWorldServerImplService">
  <port name="HelloWorldServerImplPort" binding="tns:HelloWorldServerImplPortBinding">
    <soap:address location="http://localhost:8081/helloworld"/>
  </port>
  </service>
</definitions>