Home > Java, WebService > Java RPC style Webservice with JAX-WS

Java RPC style Webservice with JAX-WS

We have to create two projects one is WebService server project and WebService client project, here client will call the methods of Server.

1. Creating Server project:

Create an interface with @WebService annotation, and annotate the method with @WebMethod whitch you wanted to expose as web service.

package com.java2practice.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 WebServiceInterface{
@WebMethod
int sum(int firstNumber, int secondNumber);
}

2. Create an implementation to the above interface  like below

package com.java2practice.ws;
import javax.jws.WebService;
@WebService(endpointInterface = "com.java2practice.ws.WebServiceInterface")
public class WebServiceImpl implements WebServiceInterface{
@Override
public int sum(int firstNumber, int secondNumber) {
return firstNumber + secondNumber;
}
}

3. Now we have to publish this webservice by using EndPoint.

package com.java2practice.ws;
import javax.xml.ws.Endpoint;
import com.java2practice.ws.WebServiceImpl;

public class WebServicePublisher{

public static void main(String[] args) {

Endpoint.publish("http://localhost:7777/webservice/testws", new WebServiceImpl());
}
}

 

  1. When we run the above code, webservice will be published and can be accessed by using http://localhost:7777/webservice/testws URL.
  2. To see the generated WSDL file click http://localhost:7777/webservice/testws?wsdl. And the generated WSDL look like
<pre>-<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->
<definitions targetNamespace="http://ws.java2practice.com/" name="WebServiceImplService"><types/>
<message name="sum"><part name="arg0" type="xsd:int"/><part name="arg1" type="xsd:int"/></message>
<message name="sumResponse"><part name="return" type="xsd:int"/></message>
<portType name="WebServiceInterface"><operation name="sum" parameterOrder="arg0 arg1">
<input wsam:Action="http://ws.java2practice.com/WebServiceInterface/sumRequest" message="tns:sum"/>
<output wsam:Action="http://ws.java2practice.com/WebServiceInterface/sumResponse" message="tns:sumResponse"/>
</operation></portType><binding name="WebServiceImplPortBinding" type="tns:WebServiceInterface">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="sum"><soap:operation soapAction=""/>
<input>
<soap:body use="literal" namespace="http://ws.java2practice.com/"/>
</input>
<output>
<soap:body use="literal" namespace="http://ws.java2practice.com/"/>
</output>
</operation>
</binding>
<service name="WebServiceImplService">
<port name="WebServiceImplPort" binding="tns:WebServiceImplPortBinding">
<soap:address location="http://localhost:7777/webservice/testws"/>
</port>
</service>
</definitions>

 

Our webservice server project is fine, now we have to create client program, for this we have to use wsimport command, this will be available in java.

  1. Go to command prompt and give wsimport it will show all the options which are there in wsimport, if it doesn’t show any thing set the path variable to jdk/bin.
  2. wsimport -keep http://localhost:7777/webservice/testws?wsdl -d D:\java2practice\Desktop\client
  3. It will generate the files, and copy those files to your IDE and create a client program like below.

package com.java2practice.ws;

public class WebServiceClient {

public static void main(String[] args) {

WebServiceImplService service = new WebServiceImplService();

WebServiceInterface serviceInterface = service.getWebServiceImplPort();

int sum = serviceInterface.sum(10, 20);

System.out.println("answer : "+sum);//outputs answer as 30

}

}

Thanks for reading.

 

Categories: Java, WebService
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: