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学习第一天学习笔记
随机推荐
- 编程中的runtime_error问题
前几天在编程中,代码如下: 头文件:ErrorHandlingModule.h //filename:ErrorHandlingModule.h #ifndef ErrorHandlingModule ...
- (10)ubuntu内核源码树
ubuntu内核源码树目录: root@ubuntu:/lib/modules/3.13.0-32-generic/build#
- [转]Google 的开源技术protobuf 简介与例子
本文来自CSDN博客:http://blog.csdn.NET/program_think/archive/2009/05/31/4229773.aspx 今天来介绍一下“Protocol Buffe ...
- HDU 1754.I Hate It-完全版线段树(单点替换、区间最值)
HDU1754.I Hate It 直接模板就可以了 代码: //B #include<iostream> #include<cstdio> #include<cstri ...
- 通过命令行上传代码到GitHub
自工作以来,本人第一次使用GitHub.下面是将本地的项目上传到GitHub的过程.上传代码的前提是:1.已注册GitHub账号:2.本地已安装Git. 第一步:远程Git仓库 进入本地的项目的根目录 ...
- [CTSC2016]时空旅行(线段树+凸包)
应该是比较套路的,但是要A掉仍然不容易. 下面理一下思路,思路清楚了也就不难写出来了. 0.显然y,z坐标是搞笑的,忽略即可. 1.如果x不变,那么直接set即可解决. 2.考虑一个空间和询问x0,通 ...
- tc: 模拟网络异常的工具-----------鸟窝
http://colobu.com/2017/04/21/tc-introduction/
- Visio中如何画面积一样的形状
如图,刚开始我想着用辅助的那些线(对齐),实现不了,后来想想直接复制就行了...........
- 三种Web前端框架比较与介绍--Vue, react, angular
一.Angular 1.MVVM(Model)(View)(View-model): 2.模块化(Module)控制器(Contoller)依赖注入: 3.双向数据绑定:界面的操作能实时反映到数据,数 ...
- 【Java】java注解@Transient的作用, 配合JPA中时间段的查询
java注解@Transient的作用 @Transient标注的属性,不会被ORM框架映射到数据库中. 用于数据库表字段和java实体属性不一致的时候,标注在属性上使用. 例如时间段的查询 查询 R ...