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学习第一天学习笔记
随机推荐
- 使用百度地图JavaScript实现驾车/公交/步行导航功能
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Delphi 的TSpeedButton按下和弹起效果
想达到这样的效果: 点击一下TSpeedButton按钮,按钮凹下去,再点击一下,按钮弹起恢复. 实现方法: 只要设置下述2个属性即可,不需要编码: ①AllowAllUp = True ②Group ...
- 51nod 1649.齐头并进-最短路(Dijkstra)
1649 齐头并进 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 在一个叫奥斯汀的城市,有n个小镇(从1到n编号),这些小镇通过 ...
- Job的使用
1.Job完成状态监听: job.addJobChangeListener(new JobChangeAdapter() { @Override publi ...
- 树链剖分【p1505】[国家集训队]旅游
Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间 ...
- [POI2010]Beads
题目大意: 给定一个长度为$n(n\leq200000)$的串$S_{1\sim n}$,选择一个$l$,从$S_1$开始,将$S$分为连续的若干段,使得每一段长度为$l$.令$k$为分出来不同的子串 ...
- Filter过滤器笔记1
Filter:过滤器 Filter主要用于对用户请求进行预处理,也可以对HttpServletResponse进行后处理,是个典型的处理链.使用Filter的完整流程是:Filter对用户请求进行预处 ...
- Java this关键字详解
this 关键字用来表示当前对象本身,或当前类的一个实例,通过 this 可以调用本对象的所有方法和属性.例如: public class Demo{ public int x = 10; publi ...
- 彻底解决INSTALL_FAILED_UPDATE_INCOMPATIBLE的安装错误、安装包与之前设备上的安装包签名不一致
有时候开发的问题:会遇到在公司上班的时候,公司的IDE能跑程序,把程序拷贝回家,再跑一次,就会出现以下错误: INSTALL_FAILED_UPDATE_INCOMPATIBLE 原因:就是你的安装包 ...
- Kyle 的 iOS 面试题
1.简单介绍下你对swizzling方法的了解,一般你什么时候使用. 2.有三个对象 A,B,C..:A retain B, B retain C, C retain B..当 A release B ...