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

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

时间: 2024-08-01 10:33:07

Web Service学习之服务端搭建与客户端调用的相关文章

C# WCF服务端搭建和客户端调用

1) 打开Visual Studio 2012,在菜单上点击文件->新建->项目->WCF服务应用程序.在弹出界面的"名称"对应的文本框中输入"WcfServiceTest",然后点击"确定"按钮. 如下图: 2) 把Visual Studio 2012自动生成的两个文件IService1.cs改名为IService.cs接口类.Service1.cs改名为Service.cs实现类. 如下图: 3) 由于Visual Stud

Spring Cloud 服务端注册与客户端调用

Spring Cloud 服务端注册与客户端调用 上一篇中,我们已经把Spring Cloud的服务注册中心Eureka搭建起来了,这一章,我们讲解如何将服务注册到Eureka,以及客户端如何调用服务. 一.注册服务 首先要再项目中引入Eureka Client,在pom.xml中加入如下配置: <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cl

yum仓库服务端搭建、客户端使用

yum源工作原理:实质是更换下载地址 ********  yum仓库服务端搭建  ******** 系统环境 服务端:10.0.0.61 [[email protected] ~]# cat /etc/redhat-release CentOS release 6.8 (Final) [[email protected] ~]# uname -r 2.6.32-642.el6.x86_64 1.安装createrepo软件: yum -y install createrepo 2.创建yum仓库

jax-ws服务端生成和客户端调用实例

最近公司让基于别的公司的接口写一个自己的接口,接口对于别的公司来说相当于客户端,但对于自己本公司要调用的程序来说,又相当于服务端. 闲来无事,总结下. 先从java自带的service开始 JAX-WS(java api xml webservice)  基于javaAPI的webService实现 用的工具是myeclipse 首先是服务端 1.新建webProject  jax-wsServer  如图: 2.新建包和类如下图 既然是入门例子,咱就越简单越好,关键看好流程 IMService

jax-ws服务端生成和客户端调用实例详解

长时间不写博客,照片都忘记怎么上传了,由于上一篇图片没有上传,所以删掉,重做了一个. 最近公司让基于别的公司的接口写一个自己的接口,接口对于别的公司来说相当于客户端,但对于自己本公司要调用的程序来说,又相当于服务端. 闲来无事,总结下. 先从java自带的service开始 JAX-WS(java api xml webservice)  基于javaAPI的webService实现 用的工具是myeclipse 首先是服务端 新建webProject  jax-wsServer  如图: 新建

[Axis2与Eclipse整合开发Web Service系列之三] 服务端返回值

前言 在前面的三篇中 [Axis2与Eclipse整合开发Web Service系列之一] 生成Web Service Client(将WSDl 转化成 Java代码) [Axis2与Eclipse整合开发Web Service系列之二] Top-Down方式,通过WSDL逆向生成服务端 [Axis2与Eclipse整合开发Web Service系列之二] Top-Down方式,通过WSDL逆向生成服务端(续) 介绍了如何使用 axis2 与 eclipse 的开发web Service .在第

Android Web Service学习总结(二)

上篇文章做好了准备工作,现在就实践(android平台调用web service实现号码归属地查询) 1.    Ksoap2-android简介 在Android平台调用web service需要依赖于第三方类库ksoap2,它是一个SOAP Web service客户端开发包,主要用于资源受限制的Java环境如Applets或J2ME应用程序(CLDC/ CDC/MIDP).在Android平台中我们并不会直接使用ksoap2,而是使用ksoap2 android.KSoap2 Androi

Android Web Service学习总结(一)

最近学习android平台调用webWebService,学习了一篇不错的博客(http://blog.csdn.net/lyq8479/article/details/6428288),可惜是2011年时的方法,而不适合现在android4.0之后的android版本,所以通过一番学习和研究,总结如下. web Service简介 通俗的理解:通过使用WebService,我们能够像调用本地方法一样去调用远程服务器上的方法.我们并不需要关心远程的那个方法是Java写的,还是PHP或C#写的:我

Web Service学习笔记:动态调用WebService

原文:Web Service学习笔记:动态调用WebService 多数时候我们通过 "添加 Web 引用..." 创建客户端代理类的方式调用WebService,但在某些情况下我们可能需要在程序运行期间动态调用一个未知的服务.在 .NET Framework 的 System.Web.Services.Description 命名空间中有我们需要的东西. 具体步骤: 1. 从目标 URL 下载 WSDL 数据. 2. 使用 ServiceDescription 创建和格式化 WSDL