Archive for the ‘WebService’ Category

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.

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

@SOAPBinding(style = Style.RPC)
public interface WebServiceInterface{
int sum(int firstNumber, int secondNumber);

2. Create an implementation to the above interface  like below

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

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


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 RI's version is JAX-WS RI 2.2.4-b01. -->
<!-- Generated by JAX-WS RI at RI's version is JAX-WS RI 2.2.4-b01. -->
<definitions targetNamespace="" 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="" message="tns:sum"/>
<output wsam:Action="" message="tns:sumResponse"/>
</operation></portType><binding name="WebServiceImplPortBinding" type="tns:WebServiceInterface">
<soap:binding transport="" style="rpc"/>
<operation name="sum"><soap:operation soapAction=""/>
<soap:body use="literal" namespace=""/>
<soap:body use="literal" namespace=""/>
<service name="WebServiceImplService">
<port name="WebServiceImplPort" binding="tns:WebServiceImplPortBinding">
<soap:address location="http://localhost:7777/webservice/testws"/>


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.


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

Java Webservice with Axis

WebServices with Apache Axis:

A)     Creating the WebService Server.

  1. Create a java dynamic web project with eclipse    page1
    2.  Create Java Class, this class will contain a method which converts a given string into upper case string, very small method. We will call this method from the webservice client program.

     package com;
    public class TestWebService {
    public String getName(String name){
    return name.toUpperCase();

3. Righ click on the class select New -> WebServices  -> WebService


4. Click Next -> Next


5. Click on Start Server leave everything as default and click finish


6. Now check http://localhost:8080/WebServiceTest/services/TestWebService?wsdl in browser

It will give you the WSDL file

<wsdl:definitions targetNamespace="http://com"><!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)--><wsdl:types><schema elementFormDefault="qualified" targetNamespace="http://com"><element name="getName"><complexType><sequence><element name="name" type="xsd:string"/></sequence></complexType></element><element name="getNameResponse"><complexType><sequence><element name="getNameReturn" type="xsd:string"/></sequence></complexType></element></schema></wsdl:types><wsdl:message name="getNameRequest"><wsdl:part element="impl:getName" name="parameters">

</wsdl:part></wsdl:message><wsdl:message name="getNameResponse"><wsdl:part element="impl:getNameResponse" name="parameters">

</wsdl:part></wsdl:message><wsdl:portType name="TestWebService"><wsdl:operation name="getName"><wsdl:input message="impl:getNameRequest" name="getNameRequest">

</wsdl:input><wsdl:output message="impl:getNameResponse" name="getNameResponse">

</wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="TestWebServiceSoapBinding" type="impl:TestWebService"><wsdlsoap:binding style="document" transport=""/><wsdl:operation name="getName"><wsdlsoap:operation soapAction=""/><wsdl:input name="getNameRequest"><wsdlsoap:body use="literal"/></wsdl:input><wsdl:output name="getNameResponse"><wsdlsoap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="TestWebServiceService"><wsdl:port binding="impl:TestWebServiceSoapBinding" name="TestWebService"><wsdlsoap:address location="http://localhost:8080/WebServiceTest/services/TestWebService"/></wsdl:port></wsdl:service></wsdl:definitions>

B)     Creating the WebService Client

  1. Right click and select New-> WebService->WebServiceClientpage5
    2. Paste the WSDL path in server definition and change the client project to WebServiceTestClient( you can give anything as a name)
    3. Click Next and Finish
    4. It will create a new java dynamic project with named as WebServiceTestClient and some java files


5. Open and write a main() method in that

public static void main(String... args){
    TestWebServiceProxy proxy = new TestWebServiceProxy();
    String name = proxy.getName("webservice");
    System.out.println("name -->"+name);
   }catch(RemoteException re){

C)     Running the client

Right click on and run as java application, it will print the result in Uppercase.

Categories: Java, WebService