Web Service學習之服務端搭建與客戶端調用

工做中用到了Web Service,可是對這塊不是很熟悉,決定花時間學習一下,如今記錄一下最基本的入門知識點。java

使用Java搭建Web Service服務端,使用Python腳本調用接口。python

 

1、Web Service服務端web

一、在Eclipse中新建一個Java工程,新建test.TestWebService類shell

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

二、運行該main函數瀏覽器

三、訪問接口app

打開瀏覽器,進入http://localhost:9999/Service/TestWebService?wsdlsvn

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>

2、Python調用函數

一、建立一個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")

二、執行Python腳本,能夠看到輸出:url

Hello,HuangJia


說明:

咱們使用Java提供了一個Web Service接口,提供一個sayHello()方法,而且接收一個String類型參數。經過Python來調用該Web Service的sayHello()方法,因此完成了一個跨服務、跨語言的調用。


本文原文地址:網祠網Web Service學習之服務端搭建與客戶端調用

相關文章
相關標籤/搜索