Sunday, February 17, 2013

Using Python Client with Java webservices (JAX-WS 2.0)

In this tutorial I am going to demonstrate you the development and deployment of the web service using Java JAX-WS API and consuming it with the help of the Python client.
Python provide many packages to support SOAP based web services,we will look one of them named suds to devlop our client.
Suds is a lightweight SOAP python client that provides a service proxy for Web can download suds from
installation of suds is pretty straightforward using fallowing methods
Here are the basic instructions for 3 different installation methods:

Using pip:
* Have the 'pip' package installed.
* Run 'pip install suds-jurko'.

Using easy-install:
* Have the 'distribute' package installed.
* Run 'easy_install suds-jurko'.

From sources:
* Unpack the source package somewhere.
* Run 'python install' from the source distribution's top level

Now come to the web service development using JAX-WS.
Step 1: create a dynamic webproject in eclipse.
Step 2: download the JAX-WS api from after download unzip the binaries in a folder and look for the lib folder inside that,copy all the jars file inside the lib folder to our already created project's WEB-INF\lib folder
Step 3: create a bean with the name of IdealMatch inside the src folder as fallows:
package com.rajkrrsinghblogspot.beans;


public class IdealMatch implements Serializable {
 private int age;
 private double salary;
 private String name;
 private String address;
 public int getAge() {
  return age;
 public void setAge(int age) {
  this.age = age;
 public double getSalary() {
  return salary;
 public void setSalary(double salary) {
  this.salary = salary;
 public String getName() {
  return name;
 public void setName(String name) { = name;
 public String getAddress() {
  return address;
 public void setAddress(String address) {
  this.address = address;
Step 4:Now create your service interface and annotate it with @webservice and @webmethod annotations as fallows

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

import com.rajkrrsinghblogspot.beans.IdealMatch;

@SOAPBinding(style = Style.RPC)
public interface IdealMatchFinder {
 @WebMethod  IdealMatch findIdealMatch();


Step 5: provide the implementation for the service interface as shown

import javax.jws.WebService;

import com.rajkrrsinghblogspot.beans.IdealMatch;

public class IdealMatchFinderImpl implements IdealMatchFinder {

 public IdealMatch findIdealMatch() {
  System.out.println(":::::::: Ideal Match Finder criteria without args ::::::::");
  IdealMatch iMatch = new IdealMatch();
  iMatch.setName("Ajay Singh");
  return iMatch;


Step 6: create one file inside WEB-INF with the name of sun-jaxws.xml and configure it to provide end point,service name,implementation and url information.
<?xml version="1.0" encoding="UTF-8"?>

<endpoints xmlns='' version='2.0'>

Step 7: now configure your web.xml to add listner WSServletContextListener
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">

lets look at the project structure to check whether all components are in place:

Step 8: Now deploy the application on server and access the url http://localhost:8083/MatchMakerService/findMatch?wsdl

it means our service is up and running now we need to have a client to access this service,we are using python client in our application,lets create our python client.
Step 9: create your python client as fallows:
import suds

class Client:
    def __init__(self):
        self.client = suds.client.Client("http://localhost:8083/MatchMakerService/findMatch?wsdl")

    def get_ideal_match(self):
        return self.client.service.findIdealMatch()

if(__name__ == "__main__"):
    client = Client()
    idealMatch = client.get_ideal_match()
    print idealMatch
save it with the extension of .py and run using python command.After run you will find the service return on the shell