Python WS服务器和Java WS客户端

3
我已经使用 pywssuds 实现了一个简单的 Web 服务服务器。
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程序中。

1个回答

0
你必须创建至少一个包含主方法的类。因此,在编译后,可以使用类似以下命令:

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);

}

这并不访问网络服务,它只是实现了该方法。 - DrKaoliN

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接