Web Service学习之服务端搭建与客户端调用
jopen
9年前
工作中用到了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()); } }
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")
2、执行Python脚本,可以看到输出:
Hello,HuangJia
说明:
我们使用Java提供了一个Web Service接口,提供一个sayHello()方法,并且接收一个String类型参数。通过Python来调用该Web Service的sayHello()方法,所以完成了一个跨服务、跨语言的调用。
本文原文地址:网祠网 > Web Service学习之服务端搭建与客户端调用