Example of using python to call java Webservice

Source: Internet
Author: User
This article describes how to call java's Webservice using python, including java and python implementation code. For more information, see I. java end
First of all, I use the server and release programs that are compiled by java's webservice support package. the code is as follows.
Webservice interface code:

The code is as follows:

Package com. xxx. test. ws;

Import javax. jws. WebMethod;
Import javax. jws. WebService;

/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-3-5
* Time: PM
*/
@ WebService (targetNamespace = "http://xxx.com/wsdl ")
Public interface CalculatorWs {
@ WebMethod
Public int sum (int add1, int add2 );

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


Interface implementation code:

The code is as follows:

Package com. xxx. test. ws;
Import javax. jws. WebService;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-3-5
* Time: PM
*/
@ WebService (
PortName = "CalculatorPort ",
ServiceName = "CalculatorService ",
TargetNamespace = "http://xxx.com/wsdl ",
EndpointInterface = "com. xxx. test. ws. CalculatorWs ")
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 the Webservice code :[Code]
Package com. xxx. test. endpoint;
Import com. xxx. test. ws. Calculator;
Import javax. xml. ws. Endpoint;

/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-3-10
* Time: PM
*/
Public class CalclulatorPublisher {
Public static void main (String [] args ){
Endpoint. publish ("http: // localhost: 8080/test/calc", new Calculator ());
// Endpoint. publish ("http: // 10.3.18.44: 8080/test/calc", new Calculator ());
}
} [/Code]
Run the above code to run your webservice. then you can use Python to test your webservice code.
After the above code runs, you can directly access it using a browser:

The code is as follows:

Http: // localhost: 8080/test/calc? Wsdl


To verify whether the startup is successful.
II. python
The following is the python test 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 = suds. client. Client (url)
Service = client. service

Print client

Sum_result = service. sum (10, 34)
Print sum_result
Print client. last_received ()

Multiply_result = service. multiply (5, 5)
Print multiply_result
Print client. last_received ()

Save the preceding code as a webservice. py file, and then modify the executable permissions:

The code is as follows:

Chmod + x webservice. py

The output result is as follows:

The code is as follows:

Suds (https://fedorahosted.org/suds/) version: 0.3.9 (beta) build: R658-20100210

Service (CalculatorService) tns = "http://xxx.com/wsdl"
Prefixes (1)
Ns0 = "http://xxx.com/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




44



25




25


III. FAQs

Note: when executing the above code, you may prompt:

The code is as follows:

Traceback (most recent call last ):
File "ws. py", line 1, in
Import suds
ImportError: No module named suds


If the dependent package is missing, we can manually download and install the suds package.

The code is as follows:

Wget http://downloads.sourceforge.net/project/python-suds/suds/0.3.9/suds-0.3.9.tar.gz? R = http % 3A % 2F % 2Fsourceforge.net % 2 Fprojects % 2Fpython-suds % 2 Ffiles % 2F & ts = 1394436413 & use_mirror = nchc
Tar zxvf suds-0.3.9.tar.gz
Cd suds-0.3.9
Sudo python setup. py install

OK.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.