Archive

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.

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.

 

Advertisements
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

page2

4. Click Next -> Next

page3

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

page4

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="http://schemas.xmlsoap.org/soap/http"/><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

page6

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

TestWebServiceProxy.java

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

C)     Running the client

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

Categories: Java, WebService