gpt4 book ai didi

java - Python WS 服务器和 Java WS 客户端

转载 作者:行者123 更新时间:2023-11-28 19:24:27 28 4
gpt4 key购买 nike

我用 pyws 实现了一个简单的 Web 服务服务器和 suds .

from pyws.server import SoapServer 
from pyws.functions.register import register

server = SoapServer(
service_name = 'Test',
tns = 'http://example.com',
location = 'http://localhost:8000/soa/',
)

@register()
def add_simple(a, b):
return a + b

这是我在 python shell 中访问它的方式:

>>> import suds
>>> client = suds.client.Client('http://localhost:8000/soa/wsdl', cache=None)
>>> client.service.add_simple('hello ', 'world')
hello world

访问 http://localhost:8000/soa 会产生这个:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>None</faultstring>
<detail>
<name>XMLSyntaxError</name>
<prefix>lxml.etree</prefix>
<exceptionName>comtypes.example.Error</exceptionName>
<params>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</params>
<message>None</message>
<type>Client</type>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>

http://localhost:8000/soa/wsdl:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:types="http://example.comtypes/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://example.com" name="Test">
<wsdl:types>
<xsd:schema elementFormDefault="qualified" targetNamespace="http://example.comtypes/" attributeFormDefault="qualified" xmlns="http://example.comtypes/">
<xsd:element name="add_simple">
<xsd:complexType>
<xsd:sequence>
<xsd:element type="xsd:string" name="a" nillable="true"/>
<xsd:element type="xsd:string" name="b" nillable="true"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="add_simple_result">
<xsd:complexType>
<xsd:sequence>
<xsd:element type="xsd:string" name="result" nillable="true"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Error">
<xsd:complexType>
<xsd:sequence/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<xsd:schema targetNamespace="http://example.com">
<xsd:import namespace="http://example.comtypes/"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="error">
<wsdl:part name="fault" element="types:Error"/>
</wsdl:message>
<wsdl:message name="add_simple">
<wsdl:part name="parameters" element="types:add_simple"/>
</wsdl:message>
<wsdl:message name="add_simple_result">
<wsdl:part name="parameters" element="types:add_simple_result"/>
</wsdl:message>
<wsdl:portType name="TestPortType">
<wsdl:operation name="add_simple">
<wsdl:input message="tns:add_simple"/>
<wsdl:output message="tns:add_simple_result"/>
<wsdl:fault message="tns:error" name="error"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding type="tns:TestPortType" name="TestBinding">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="add_simple">
<soap:operation soapAction="http://example.comadd_simple"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
<wsdl:fault name="error">
<soap:fault use="literal" name="error"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TestService">
<wsdl:port binding="tns:TestBinding" name="TestPort">
<soap:address location="http://localhost:8000/soa/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

如何从 java(为简单起见)控制台应用程序访问此服务?我之所以问这个问题,通常是因为在 Java 客户端中,您会创建一个您尝试使用的类的实例,但在这种情况下不适用。
我需要指出的是,我已经使用 eclipse 网络服务浏览器测试了这个网络服务并且它有效 - 我只是不知道如何将它集成到 java 程序中。

最佳答案

您必须至少创建一个具有 main 方法的类。所以在编译之后,你可以使用这样的命令:

java className localhost:8000/soa/wsdl hello world

public static void main(String[] args){

String host = args[0];
String hello = args[1];
String world = args[2];

System.out.println(hello+" "+world);

}

关于java - Python WS 服务器和 Java WS 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16523828/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com