web service的定义
来源:互联网 发布:自学java知乎 编辑:程序博客网 时间:2024/06/05 07:42
目前对Web Service没有统一的定义,定义一:Web Service是自包含的、模块化的应用程序,它可以在Web中被描述、发布、查找以及调用。定义二:Web Service是基于网络的、分布式的模块化组件,它执行特定的任务,遵守具体的技术规范,这些规范使得Web Service能与其他兼任的组件进行操作。定义三:所谓Web Service是指由企业发布的完成其特别商务需求的在线应用服务,其他公司或应用软件通过Internet来访问并使用这项应用服务。笔者比较喜欢维基百科的定义:Web Service是一种面向服务的架构的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作。
目前对WebService还没有绝对全面和准确的定义,但是一般Web Service通常包括:
SOAP
一个基于XML的可扩展消息信封格式,需同时绑定一个传输协议。这个协议通常是HTTP或HTTPS、SMTP、XMPP。
WSDL
一个XML格式文档,用以描述服务端口访问方式和使用协议的细节。通常用来辅助生成服务器和客户端代码及配置信息。比如JAVA语言的wsdl2java 工具。
UDDI
一个用来发布和搜索WEB服务的协议,应用程序可借由此协议在设计或运行时找到目标WEB Service。
2. 使用Web Service的方式
远端过程调用
REST式服务
表达性状态转移(Representational state transfer,REST)类似于HTTP协议,REST把接口限定在一组HTTP操作中,比如GTP、PUT、DELETE等以供调用,此种服务可以通过WSDL来描述SOAP消息内容,通过HTTP限定动作接口;或者完全在SOAP中对动作进行抽象。
编写Web Service
public interface HelloWorld {
String sayHi(@WebParam(name=”text”) String text);
//这里的text为输入参数
package demo.hw.server;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.jws.WebService;
@WebService(endpointInterface = “demo.hw.server.HelloWorld”, serviceName = “HelloWorld”)
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
System.out.println(“sayHi called”);
return “Hello ” + text;
}
}
System.out.println(“Starting Server”);
HelloWorldImpl implementor = new HelloWorldImpl();
String address = “http:
//localhost:9000/helloWorld”; //发布地址
Endpoint.publish(address, implementor);
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(HelloWorld.class);
factory.setAddress(“http:
//localhost:9000/helloWorld”); //地址为以上发布的地址
HelloWorld client = (HelloWorld) factory.create();
String reply = client.sayHi(“HI”);
System.out.println(“Server said: ” + reply);
两者都是Web Service资源表示方式,直白点说,就是Web Service将以两者之一来请求或者返回结果。它们定义如下:
1. JSON 一种轻量级的数据交换格式,具有良好的可读性和便于快速编写的特性。另外,JSON采用的兼容性很高的文本格式。
2. XML 扩展标记语言,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。XML、非常适合Web传输,其提供了一种统一方法来描述和交换独立于应用程序或供应商的结构化数据。
XML与JSON优缺点比较:
1. 可读性 两者不相上下,JSON格式{property1:value1,property2:value2,…},XML格式value1value2。在描述上,XML要优于JSON。
2. 可扩展性 这点XML可以做的,JSON也可以。
3. 数据大小 这点JSON要优于XML,但是差别不是太大。
4. 使用范围 XML目前被业界广泛采用,而javascript语言对JSON支持要比XML优越。
5. 性能 经测试,JSON的解析速度几乎是XML解析的10倍。
HelloWorld.java
package demo.spring.service;
import javax.jws.WebService;
@WebService public interface HelloWorld {
String sayHi(String text);
package demo.spring.service;
import javax.jws.WebService;
@WebService(endpointInterface = “demo.spring.service.HelloWorld”)
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
System.out.println(“sayHi called”);
return “Hello ” + text;
}
}
在项目的WEB-INF目录下新建一个”cxf-servlet.xml”文件,内容:
//配置文件
org.springframework.web.context.ContextLoaderListener
最后在上述的beans.xml在创建客户端。
这里可以选择将”helloClient” 注入到Srping bean中,或者采用手动的方式从Spring application context中查找:
ApplicationContext context = …;
// your Spring ApplicationContext
HellWorld client = (HelloWorld) context.getBean(“helloClient”);
目前对WebService还没有绝对全面和准确的定义,但是一般Web Service通常包括:
SOAP
一个基于XML的可扩展消息信封格式,需同时绑定一个传输协议。这个协议通常是HTTP或HTTPS、SMTP、XMPP。
WSDL
一个XML格式文档,用以描述服务端口访问方式和使用协议的细节。通常用来辅助生成服务器和客户端代码及配置信息。比如JAVA语言的wsdl2java 工具。
UDDI
一个用来发布和搜索WEB服务的协议,应用程序可借由此协议在设计或运行时找到目标WEB Service。
2. 使用Web Service的方式
远端过程调用
Web Service提供一个分布式函数或方法接口供用户调用,这是一种比较传统的方式。通常,在WSDL中对RPC接口进行定义。尽管最初的Web Service广泛采用RPC这种方式部署,但是针对其过于紧密之耦合性的批评声也络绎不绝。原因是RPC式的Web Service服务是利用一个简单的映射,把用户请求直接转换为一个特定语言编写的函数或方法。
面向服务架构
相比RPC式的Web Service,面向服务架构(SOA)得到了大部分主要软件供应商以及业界专家的支持和肯定。因为SOA方式更加关注如何去连接服务而不是去特地某个实现的细节。
REST式服务
表达性状态转移(Representational state transfer,REST)类似于HTTP协议,REST把接口限定在一组HTTP操作中,比如GTP、PUT、DELETE等以供调用,此种服务可以通过WSDL来描述SOAP消息内容,通过HTTP限定动作接口;或者完全在SOAP中对动作进行抽象。
3. HelloWorld
笔者将使用Apache Cxf官网的How To示例来实现helloworld和后续的示例,并且笔者将使用maven工具来构建示例项目。关于cxf及maven请参考官网http://cxf.apache.org、http://maven.apache.org。(示例使用maven构建项目pom.xml)编写Web Service
HelloWorld.java
public interface HelloWorld {
String sayHi(@WebParam(name=”text”) String text);
//这里的text为输入参数
}
package demo.hw.server;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.jws.WebService;
@WebService(endpointInterface = “demo.hw.server.HelloWorld”, serviceName = “HelloWorld”)
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
System.out.println(“sayHi called”);
return “Hello ” + text;
}
}
发布服务
System.out.println(“Starting Server”);
HelloWorldImpl implementor = new HelloWorldImpl();
String address = “http:
//localhost:9000/helloWorld”; //发布地址
Endpoint.publish(address, implementor);
访问Web Service
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(HelloWorld.class);
factory.setAddress(“http:
//localhost:9000/helloWorld”); //地址为以上发布的地址
HelloWorld client = (HelloWorld) factory.create();
String reply = client.sayHi(“HI”);
System.out.println(“Server said: ” + reply);
System.exit(0);
两者都是Web Service资源表示方式,直白点说,就是Web Service将以两者之一来请求或者返回结果。它们定义如下:
1. JSON 一种轻量级的数据交换格式,具有良好的可读性和便于快速编写的特性。另外,JSON采用的兼容性很高的文本格式。
2. XML 扩展标记语言,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。XML、非常适合Web传输,其提供了一种统一方法来描述和交换独立于应用程序或供应商的结构化数据。
XML与JSON优缺点比较:
1. 可读性 两者不相上下,JSON格式{property1:value1,property2:value2,…},XML格式value1value2。在描述上,XML要优于JSON。
2. 可扩展性 这点XML可以做的,JSON也可以。
3. 数据大小 这点JSON要优于XML,但是差别不是太大。
4. 使用范围 XML目前被业界广泛采用,而javascript语言对JSON支持要比XML优越。
5. 性能 经测试,JSON的解析速度几乎是XML解析的10倍。
4. CXF与Spring框架集成
假定使用Maven来构建项目,pom.xml
HelloWorld.java
package demo.spring.service;
import javax.jws.WebService;
@WebService public interface HelloWorld {
String sayHi(String text);
}
package demo.spring.service;
import javax.jws.WebService;
@WebService(endpointInterface = “demo.spring.service.HelloWorld”)
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
System.out.println(“sayHi called”);
return “Hello ” + text;
}
}
在项目的WEB-INF目录下新建一个”cxf-servlet.xml”文件,内容:
另外还需要设置Servlet。一般,Web项目中都会有一个Web.xml文件,我们需要添加Spring ContextLoaderLister元素。当Web容器启动时,将由Spring ContextLoaderLister来启动Srping,并且加载配置文件。这些配置文件是通过context-param元素来定义的。
//配置文件
org.springframework.web.context.ContextLoaderListener
最后在上述的beans.xml在创建客户端。
这里可以选择将”helloClient” 注入到Srping bean中,或者采用手动的方式从Spring application context中查找:
ApplicationContext context = …;
// your Spring ApplicationContext
HellWorld client = (HelloWorld) context.getBean(“helloClient”);
0 0
- web service的定义
- Web Service的基本定义
- 定义良好的web service接口
- web service 的定义步骤(阅读笔记)
- 【转载】Web Service定义
- 架构Web Service:交互界面,Web服务定义的核心
- 定义web service接口的十点注意事项(转)
- 定义web service接口的十点注意事项
- web service的基础教程 web Service java
- web service的基础教程 web Service java
- web service的基础教程 web Service java
- web service的基础教程 web Service java
- web service的基础教程 web Service java
- Web Service的加密
- Web Service 的特点
- Web Service的介绍
- Web Service的加密
- web service的安全
- Android XML解析器 - SAX
- 用dumpdecrypted给App砸壳
- 使用calabash-android 实现 翻转屏幕
- android studio 导入一个eclipse常见问题
- 优化页面加载速度
- web service的定义
- Android属性动画完全解析(上),初识属性动画的基本用法
- VS2015配置OpenCV3.0
- 微信公众号开发系列-获取微信OpenID
- 安装压缩版mysql5.6.25
- 机器学习笔记_ 提升
- Maven环境配置及pom.xml文件遇到的问题
- linux 下Python调用C模块
- [游戏与人生]推荐几款老单机游戏