WebService学习小结
基于web的服务,服务器整理资源供多个客户端应用访问,是一种多个跨平台跨语言的应用间通信整合的方案
使用场景:天气预报、股票、地图,火车票
schema约束复习
<!-- book.xsd,定义schema约束 -->
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema";
targetNamespace="http://www.mabaoqing.cn"; elementFormDefault="qualified">
<element name="书架">
<!-- 可以有多个不同的子标签,须在下方使用element定义· -->
<complexType>
<!-- 集合,默认最小最大均为1,设置为无最大限制 -->
<sequence maxOccurs="unbounded">
<element name="书">
<complexType>
<sequence>
<element name="书名" type="string"></element>
<element name="作者" type="string"></element>
<element name="价格" type="double"></element>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
<!-- xml文档引用schema定义的book.xsd文档 -->
<?xml version="1.0" encoding="UTF-8"?>
<书架 xmlns="http://www.mabaoqing.cn";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://www.mabaoqing.cn book.xsd">
<!--
Schema规范:★★★★★
1.所有标签和属性必须在schema中定义
2.所有schema文件必须有个id,在这里叫namespace(ns,如xmlns引入)
3.ns由targetNamespace属性指定,值是一个url(可能不存在)
定义自己的用targetNamespace,引入用xmlns属性
如果引入的schema不是w3c定义的,必须指定schema文件的位置(xsi(别名):schemaLocation),spring中常用
xmlns通常写为自己的schema,否则需要为xml中每个标签加别名前缀,如xsi:书架
-->
<书>
<书名>海底两万里</书名>
<作者>鲁宾孙</作者>
<价格>58.0</价格>
</书>
</书架>
几个重要术语
WSDL:webservice definition language,webservice定义语言
- 定义了webservice的服务器段和客户端应用交互传递请求和响应数据的格式和方式。
- 一个webservice对应一个唯一的wsdl文档。
SOAP:simple object access protocal,简单对象访问协议
- 一种简单的,基于HTTP、XML的网络传输协议,用于在web上交换结构化的数据。
- soap消息:请求和响应消息
SEI:webservice endpoint interface,webservice终端接口
- 就是webservice用来处理请求的接口
CXF:Celtix + XFires,一个apache的用于开发webservice服务器和客户端的框架。
WebService请求流程

wsdl基本结构
wsdl:definitions:根标签
- wsdl:types:schema定义了标签结构
wsdl:message:用来定义消息的结构
- part:引用types中定义的标签片段
wsdl:portType:用来定义服务器端的SEI
- operation:用来指定SEI中的方法
- input/output:引用message
- operation:用来指定SEI中的方法
wsdl:binding:定义SEI的实现类(type属性引用portType标签)
wsdl:service: 服务器端的一个webservice容器
name:用于指定容器类(get方法)
port:用于指定一个服务器处理请求的入口(SEI的实现)
- binding属性引用上面定义的binding
- address:服务器地址
JDK1.6+开发WebService
- 接口:@WebService类注解,@WebMethod方法注解
- 实现:@WebService类注解
- 发布:EndPoint.publish(address, implmentor);
根据wsdl调用webservice
cmd命令行进入相应目录,使用以下命令生成服务器端提供的类源文件:
wsimport -keep url/file(jdk)
wsdl2java url/file(cxf,添加path路径)
java调用.net的webservice错误处理:
将wsdl文档中的所有
改为:
<s:any minOccurs="2" maxOccurs="2"/
CXF所需的jar包及依赖
<properties>
<cxf.version>3.1.12</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
CXF拦截器
分类:入/出,客户端/服务器端
客户端:ClientProxy.getClient(obj).getInInterceptors().add(new LoggingInInterceptor());
服务器:EndpointImpl endpoint = (EndpointImpl) publish;
endpoint.getOutInterceptors().add(new LoggingInInterceptor());
自定义拦截器:extends AbstractPhaseInterceptor
添加构造方法指定拦截时间
public MyInteceptor() {
super(Phase.PRE_PROTOCOL); //协议化时进行拦截
}
CXF集成Spring发布
首先配置服务器端的web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxfServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxfServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
spring.xml
<beans xmlns="http://www.springframework.org/schema/beans";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:jaxws="http://cxf.apache.org/jaxws";
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd";>
<!-- 低版本还会有个 cxf-extension-soap.xml 文件 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="orderWS" implementor="cn.mbq.cxf.spring.OrderWSImpl" address="/OrderWS" >
<!-- 配置服务器端的入拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
</jaxws:endpoint>
</beans>
然后配置客户端,首先生成服务器端代码,再添加配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans";
xmlns:jaxws="http://cxf.apache.org/jaxws"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd";>
<!-- OrderWS orderWS = context.getBean("orderClient") -->
<jaxws:client id="orderClient" serviceClass="cn.mbq.cxf.spring.OrderWS"
address="http://localhost:8080/ws_cxf_spring/OrderWS";/>
</beans>
Ajax跨域访问
localhost与ip地址不再同一域内
仍然请求本地服务器,在本地服务器请求外网资源(使用URL获取HttpURLConnection对象)
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int id = Integer.parseInt(req.getParameter("id"));
String requestData = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'; xmlns:q0='http://spring.cxf.mbq.cn/'; xmlns:xsd='http://www.w3.org/2001/XMLSchema'; xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><soapenv:Body><q0:getOrderById><arg0>"; + id + "</arg0></q0:getOrderById></soapenv:Body></soapenv:Envelope>";
URL url = new URL("http://192.168.199.203:8080/ws_cxf_spring/OrderWS";);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
OutputStream os = connection.getOutputStream();
os.write(requestData.getBytes("utf-8"));
os.flush();
int code = connection.getResponseCode();
if(code==200){
InputStream in = connection.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len=in.read(buffer))>0) {
resp.getOutputStream().write(buffer, 0, len);
}
}
}
WebService学习小结的更多相关文章
- 调用webService学习小结
这段时间项目进行到了最后时刻,但是还有很多需求没有搞清楚,眼看deadline越来越近,压力也越来越大.现在我的主要工作是将别人开发好的一个系统给加载到我们系统中,使用的方法是通过webService ...
- webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口
webService学习之路一:讲解了通过传统方式怎么发布及调用webservice webService学习之路二:讲解了SpringMVC和CXF的集成及快速发布webservice 本篇文章将讲 ...
- flex学习小结
接触到flex一个多月了,今天做一个学习小结.如果有知识错误或者意见不同的地方.欢迎交流指教. 画外音:先说一下,我是怎么接触到flex布局的.对于正在学习的童鞋们,我建议大家没事可以逛逛网站,看看人 ...
- Python 学习小结
python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...
- react学习小结(生命周期- 实例化时期 - 存在期- 销毁时期)
react学习小结 本文是我学习react的阶段性小结,如果看官你是react资深玩家,那么还请就此打住移步他处,如果你想给一些建议和指导,那么还请轻拍~ 目前团队内对react的使用非常普遍,之 ...
- objective-c基础教程——学习小结
objective-c基础教程——学习小结 提纲: 简介 与C语言相比要注意的地方 objective-c高级特性 开发工具介绍(cocoa 工具包的功能,框架,源文件组织:XCode使用介绍) ...
- pthread多线程编程的学习小结
pthread多线程编程的学习小结 pthread 同步3种方法: 1 mutex 2 条件变量 3 读写锁:支持多个线程同时读,或者一个线程写 程序员必上的开发者服务平台 —— DevSt ...
- ExtJs学习笔记之学习小结LoginDemo
ExtJs学习小结LoginDemo 1.示例:(登录界面) <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- [未完成]WebService学习第一天学习笔记
[未完成]WebService学习第一天学习笔记[未完成]WebService学习第一天学习笔记
随机推荐
- kubernetes 搭建集群外部ip服务
nginx-pod.yaml apiVersion: v1 kind: Pod metadata: name: webapp labels: app: webapp spec: containers: ...
- hdu 5105(数学题)
Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 在OpenResty中使用淘宝的concat进行css和js合并,同时支持GZIP静态压缩
=======================================================================================cd /usr/local ...
- Delphi 7 在程序中直接执行SQL脚本文件
Delphi 7 在程序中直接执行SQL脚本文件 在处理MSDE一些操作中.需要执行一些SQL脚本.有的是从SQLServer 2000中生成的SQL为后缀的脚本.在MSDE中没有企业管理器,操作都是 ...
- (30)C#Timer类
有三种Timer 1.System.Windows.Forms.Timer 应用于WinForm中,它的主要缺点是计时不精确,而且必须有消息循环,Console Application(控制台应用程 ...
- 51nod 1062 序列中最大的数【打表】
1062 序列中最大的数 题目来源: Ural 1079 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 有这样一个序列a: a[0] = 0 a[ ...
- 链式前向星DFS
本文链接:http://www.cnblogs.com/Ash-ly/p/5399057.html 采用链式前向星存图的DFS: #include <iostream> #include ...
- 详解xml
xml xml简介 XML是一种可扩展标记语言 (Extensible Markup Language, XML).是一种可扩展的标记语言,XML重在数据交换,用于不同平台或者应用程序之间交换数据,存 ...
- 差分【p3948】 数据结构
顾z 你没有发现两个字里的blog都不一样嘛 qwq 题目描述-->p3948 数据结构 分析 其实这题完全没有分析的 qwq. 只是因为写了差分数组相关知识,所以顺便写一下题解 qwq. 对于 ...
- linux安装mysql数据库(5.7之前的版本)
到mysql官网下载mysql编译好的二进制安装包 解压32位安装包: 进入安装包所在目录,执行命令:tar mysql-5.6.17-linux-glibc2.5-i686.tar.gz 复 ...