Example of using python to call java Webservice

Source: Internet
Author: User

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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe Code is as follows: chmod + x webservice. py

The output result is as follows:
Copy codeThe 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
<? 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. FAQs

Note: When executing the above Code, you may prompt:
Copy codeThe Code is as follows: Traceback (most recent call last ):
File "ws. py", line 1, in <module>
Import suds
ImportError: No module named suds
If the dependent package is missing, we can manually download and install the suds package.
Copy codeThe 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.

Related Article

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.