工做中用到了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学习之服务端搭建与客户端调用