Web Service學習之服務端搭建與客戶端調用
工作中用到了Web Service,但是對這塊不是很熟悉,決定花時間學習一下,現在記錄一下最基本的入門知識點。
使用Java搭建Web Service服務端,使用Python腳本調用接口。
一、Web Service服務端
1、在Eclipse中新建一個Java工程,新建test.TestWebService類
package test; import javax.jws.WebService; import javax.xml.ws.Endpoint;@WebService public class TestWebService {
public String sayHello(String name){ return "Hello," + name; } public static void main(String[] args) { Endpoint.publish("http://localhost:9999/Service/TestWebService", new TestWebService()); }
}</pre>
2、運行該main函數
3、訪問接口
打開瀏覽器,進入http://localhost:9999/Service/TestWebService?wsdl
This XML file does not appear to have any style information associated with it. The document tree is shown below. <!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --> <!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --> <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://test/" name="TestWebServiceService"> <types> <xsd:schema> <xsd:import namespace="http://test/" schemaLocation="http://localhost:9999/Service/TestWebService?xsd=1"/> </xsd:schema> </types> <message name="sayHello"> <part name="parameters" element="tns:sayHello"/> </message> <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse"/> </message> <portType name="TestWebService"> <operation name="sayHello"> <input wsam:Action="http://test/TestWebService/sayHelloRequest" message="tns:sayHello"/> <output wsam:Action="http://test/TestWebService/sayHelloResponse" message="tns:sayHelloResponse"/> </operation> </portType> <binding name="TestWebServicePortBinding" type="tns:TestWebService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="sayHello"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="TestWebServiceService"> <port name="TestWebServicePort" binding="tns:TestWebServicePortBinding"> <soap:address location="http://localhost:9999/Service/TestWebService"/> </port> </service> </definitions>二、Python調用
1、創建一個Python腳本,鍵入下面代碼:
#!/usr/bin/python--coding:utf8--
from suds.client import Client
url = "http://localhost:9999/Service/TestWebService?wsdl"
client = Client(url)
調用Service方法傳入參數
print client.service.sayHello("HuangJia")</pre>
2、執行Python腳本,可以看到輸出:
Hello,HuangJia
說明:
我們使用Java提供了一個Web Service接口,提供一個sayHello()方法,并且接收一個String類型參數。通過Python來調用該Web Service的sayHello()方法,所以完成了一個跨服務、跨語言的調用。
本文原文地址:網祠網 > Web Service學習之服務端搭建與客戶端調用
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!