基于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
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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://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学习小结
原文地址:https://www.cnblogs.com/mabaoqing/p/10346371.html