SoFunction
Updated on 2024-11-21

python call java webservice example

I. java side
First of all, I use java comes with the support package for webservice to write the server-side and publishing program, the code is as follows.
Interface code for the webservice:

Copy Code The code is as follows.
package ;

import ;
import ;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-5
* :: Time: 3:11 p.m.
 */
@WebService(targetNamespace = "/wsdl")
public interface CalculatorWs {
    @WebMethod
    public int sum(int add1, int add2);

    @WebMethod
    public int multiply(int mul1, int mul2);
}


Interface implementation code:
Copy Code The code is as follows.
package ;
import ;
/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-5
* :: Time: 3:12 p.m.
 */
@WebService(
        portName = "CalculatorPort",
        serviceName = "CalculatorService",
        targetNamespace = "/wsdl",
        endpointInterface = "")
public class Calculator implements CalculatorWs {
    public int sum(int add1, int add2) {
        return add1 + add2;
    }

    public int multiply(int mul1, int mul2) {
        return mul1 * mul2;
    }
}


Publish Webservice Code: [code]
package ;
import ;
import ;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-10
* :: Time: 3:10 p.m.
 */
public class CalclulatorPublisher {
    public static void main(String[] args) {
        ("http://localhost:8080/test/calc", new Calculator());
        //("http://10.3.18.44:8080/test/calc", new Calculator());
    }
}[/code]
Run this code above to get your webservice running, next you can use Python to test your webservice code.
Once the above code is running, you can access it directly using your browser:

Copy Code The code is as follows.
http://localhost:8080/test/calc?wsdl

to verify that the startup was successful.
II. python end
Next is the python test code:
Copy Code The code is as follows.
#!/usr/bin/python
import suds
url = 'http://localhost:8080/test/calc?wsdl'
#url = 'http://10.3.18.44:8080/test/calc?wsdl'
client = (url)
service =

print client

sum_result = (10, 34)
print sum_result
print client.last_received()

multiply_result = (5, 5)
print multiply_result
print client.last_received()

Save the above code as a file and change the executable permissions again:

Copy Code The code is as follows.
chmod +x

The output is as follows:

Copy Code The code is as follows.
Suds ( /suds/ )  version: 0.3.9 (beta)  build: R658-20100210

Service ( CalculatorService ) tns="/wsdl"
   Prefixes (1)
      ns0 = "/wsdl"
   Ports (1):
      (CalculatorPort)
         Methods (2):
            multiply(xs:int arg0, xs:int arg1, )
            sum(xs:int arg0, xs:int arg1, )
         Types (4):
            multiply
            multiplyResponse
            sum
            sumResponse


44
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope>
   <S:Body>
      <ns2:sumResponse>
         <return>44</return>
      </ns2:sumResponse>
   </S:Body>
</S:Envelope>
25
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope>
   <S:Body>
      <ns2:multiplyResponse>
         <return>25</return>
      </ns2:multiplyResponse>
   </S:Body>
</S:Envelope>

III. Frequently asked questions

Note that it is possible to prompt when executing the code above:

Copy Code The code is as follows.
Traceback (most recent call last):
  File "", line 1, in <module>
    import suds
ImportError: No module named suds

It says that dependent packages are missing, we can download and install suds packages manually.
Copy Code The code is as follows.
wget /project/python-suds/suds/0.3.9/suds-0.3.?r=http%3A%2F%%2Fprojects%2Fpython-suds%2Ffiles%2F&ts=1394436413&use_mirror=nchc
tar zxvf suds-0.3.
cd suds-0.3.9
sudo python install

OK。