我已经用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 Web 服务浏览器测试了这个 Web 服务并且它工作正常——我只是不知道如何将它集成到 Java 程序中。