2013-05-13 78 views
3

我已經實現了一個簡單的Web服務服務器,其中pywssudsPython WS服務器和Java WS客戶端

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殼訪問:

>>> 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程序中。

回答

0

你必須用主要方法創建至少一個類。 所以編譯之後,可以使用如下命令:

Java類名本地主機:8000/SOA/WSDL世界你好

public static void main(String[] args){ 

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

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

} 
+0

這不訪問Web服務,它只是實現方法。 – DrKaoliN 2013-05-13 14:35:51

相關問題