Tuesday, August 19, 2014

JAX-WS Start From WSDL - RPC style

JAX-WS supports the end point artifacts generation for RPC style also. However note that though there is some minor differences in the kind of code generation, it is more of a theoretical discussion from the perspective of artifacts generated by Java.

Let's have a WSDL which again is for Temperature conversion but this time the WSDL is RPC style.

<definitions targetNamespace="http://ws.lalit.com/"
   xmlns="http://schemas.xmlsoap.org/wsdl/"
   xmlns:tns="http://ws.lalit.com/"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
   name="TemperatureConvertorService">

<types/>

<message name="temperatureValue">
  <part name="parameters" type="xsd:double" />
</message>

<portType name="TemperatureConvertorRPC">
  <operation name="celsiusToFarenheitOp">
    <input message="tns:temperatureValue" />
    <output message="tns:temperatureValue" />
  </operation>
  <operation name="farenheitToCelsiusOp">
    <input message="tns:temperatureValue" />
    <output message="tns:temperatureValue" />
  </operation>
</portType>

<binding name="TemperatureConvertorPortBinding" type="tns:TemperatureConvertorRPC">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
    <operation name="celsiusToFarenheitOp">
      <soap:operation soapAction="" />
      <input>
          <soap:body use="literal" namespace="http://ws.lalit.com/" />
      </input>
      <output>
           <soap:body use="literal" namespace="http://ws.lalit.com/" />
      </output>
    </operation>
    <operation name="farenheitToCelsiusOp">
      <soap:operation soapAction="" />
      <input><soap:body use="literal" namespace="http://ws.lalit.com/" />
      </input>
      <output><soap:body use="literal" namespace="http://ws.lalit.com/" />
      </output>
    </operation>
</binding>

<service name="TemperatureConvertorServiceRPC">
  <port name="TemperatureConvertorRPCPort" binding="tns:TemperatureConvertorPortBinding">
    <soap:address location="http://localhost:8081/tempConv" />
  </port>
</service>

</definitions>

Not the in message we have type in place of element.Again generate the artifact using wsimport

wsimport -keep -s <source_folder_location> -d <generated_compiled_class> <WSDL location with WSDL Name>

The generated interface looks as follows:

@WebService(name = "TemperatureConvertorRPC",     
                     targetNamespace = "http://ws.lalit.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface TemperatureConvertorRPC{

    @WebMethod
    public void celsiusToFarenheitOp(
        @WebParam(name = "parameters",
           mode = WebParam.Mode.INOUT,
          partName = "parameters")
        Holder<Double> parameters);

Note that the parameter is type Holder<T>. It is a type of inout parameter where the argument is send as Holder<T> and is returned back using the same parameter.
Implement the interface

@WebService(endpointInterface="com.lalit.ws.TemperatureConvertorRPC")
public class TemperatureConvertorRPCService implements TemperatureConvertorRPC {

  public void celsiusToFarenheitOp(Holder<Double> parameters) {
      double celsius = parameters.value;
      double fahr = 1.8*celsius +32;
      parameters.value = fahr;
  }
...

Register the endpoint

<servlet>
  <servlet-name>TemperaturConvertorRPC</servlet-name>
<servlet-class>com.oyeJava.TemperatureConvertorRPCService</servlet-class>
</servlet>

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

Deploy it and hit the WSDL. Note that the WSDL is not same as you have provided. If you want to retain the WSDL than change the implementation as follows:

@WebService(name = "TemperatureConvertorRPC",
            endpointInterface="com.lalit.ws.TemperatureConvertorRPC",
            targetNamespace = "http://ws.lalit.com/",
            wsdlLocation = "WEB-INF/wsdl/TemperatureConvertorRPCStyle.wsdl")
public class TemperatureConvertorRPCService implements TemperatureConvertorRPC {

More on Web services

No comments:

Post a Comment