搜索
首页后端开发Python教程python调用java的Webservice示例

一、java端
首先我使用的是java自带的对webservice的支持包来编写的服务端和发布程序,代码如下。
webservice的接口代码:

复制代码 代码如下:
package com.xxx.test.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-5
 * Time: 下午3:11
 */
@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);
}


接口实现代码:
复制代码 代码如下:
package com.xxx.test.ws;
import javax.jws.WebService;
/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-5
 * Time: 下午3:12
 */
@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;
    }
}


发布Webservice代码:[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: 下午3:10
 */
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]
运行上面的这段代码,让你的webservice跑起来,接下来就可以使用Python来测试你的webservice代码了。
上面的代码跑起来后,你可以直接使用浏览器访问:

复制代码 代码如下:
http://localhost:8080/test/calc?wsdl
来验证是否启动成功。
二、python端
接下来是python的测试代码:
复制代码 代码如下:
#!/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()

将上述代码保存成webservice.py文件,再修改一下可执行权限:

复制代码 代码如下:
chmod +x webservice.py

输出结果如下:

复制代码 代码如下:
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
     

  

三、常见问题

注意,在执行上面的代码时,有可能提示:

复制代码 代码如下:
Traceback (most recent call last):
  File "ws.py", line 1, in
    import suds
ImportError: No module named suds
说缺少依赖的包,我们可以手工下载安装suds包。
复制代码 代码如下:
wget http://downloads.sourceforge.net/project/python-suds/suds/0.3.9/suds-0.3.9.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fpython-suds%2Ffiles%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。

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
列表和阵列之间的选择如何影响涉及大型数据集的Python应用程序的整体性能?列表和阵列之间的选择如何影响涉及大型数据集的Python应用程序的整体性能?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

说明如何将内存分配给Python中的列表与数组。说明如何将内存分配给Python中的列表与数组。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python数组中指定元素的数据类型?您如何在Python数组中指定元素的数据类型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

什么是Numpy,为什么对于Python中的数值计算很重要?什么是Numpy,为什么对于Python中的数值计算很重要?May 03, 2025 am 12:03 AM

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

讨论'连续内存分配”的概念及其对数组的重要性。讨论'连续内存分配”的概念及其对数组的重要性。May 03, 2025 am 12:01 AM

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

在Numpy阵列上可以执行哪些常见操作?在Numpy阵列上可以执行哪些常见操作?May 02, 2025 am 12:09 AM

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Python的数据分析中如何使用阵列?Python的数据分析中如何使用阵列?May 02, 2025 am 12:09 AM

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器