WEBService及Apache的CXf框架
一.WEBService
1.WEBService提供远程服务调用.
2.WEBService 架构

3.WEBService的特点
- WebService通过HTTP POST方式接受客户的请求
- WebService与客户端之间一般使用SOAP协议传输XML数据
- 它本身就是为了跨平台或跨语言而设计的
4.SOAP和WSDL
- SOAP(Simple Object Access Protocol) ,简单对象访问协议.
SOAP作为一个基于XML语言的协议用于在网上传输数据。
SOAP = 在HTTP的基础上+XML数据。
SOAP是基于HTTP的。
uSOAP的组成如下:
Envelope – 必须的部分。以XML的根元素出现。
Headers – 可选的。
Body – 必须的。在body部分,包含要执行的服务器的方法。和发送到服务器的数据。
POST /WebServices/IpAddressSearchWebService.asmx HTTP/1.1 Host: ws.webxml.com.cn Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://WebXml.com.cn/getCountryCityByIp" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getCountryCityByIp xmlns="http://WebXml.com.cn/"> <theIpAddress>string</theIpAddress> </getCountryCityByIp> </soap:Body> </soap:Envelope> - (WSDL)Web服务描述语言
就是一个xml文档,用于描述当前服务的一些信息(服务名称、服务的发布地址、服务提供的方法、方法的参数类型、方法的返回值类型等)
5.基于JDK发布一个WEBService服务.
- 服务端的发布
第一步:创建一个Java项目
第二步:创建一个类,加入Webservice注解
第三步:提供一个方法sayHello
第四步:在main方法中调用jdk提供的发布服务的方法
第五步:访问服务的wsdl文档(服务的发布地址+?wsdl)http://192.168.115.87:8080/hello?wsdl
@WebService public class HelloService { public String sayHello(String name,int i){ System.out.println("服务端的sayHello方法被调用了。。。。"); return "helle" + name; } public static void main(String[] args) { String address = "http://192.168.115.87:8080/hello"; Object implementor = new HelloService(); Endpoint.publish(address, implementor); } }
2.客户端的调用
2.1 在jdk中使用wsimport命令
作用:解析wsdl,生成本地代码

2.2 客户端的调用
1、使用wsimport命令解析wsdl文件生成本地代码
2、通过本地代码创建一个代理对象
3、通过代理对象实现远程调用
**
* 1、使用wsimport命令解析wsdl文件生成本地代码
* 2、通过本地代码创建一个代理对象
* 3、通过代理对象实现远程调用
* @author zhaoqx
*
*/
public class App {
public static void main(String[] args) {
HelloServiceService ss = new HelloServiceService();
//创建客户端代理对象,用于远程调用
HelloService proxy = ss.getHelloServicePort();
String ret = proxy.sayHello("小明", 10);
System.out.println(ret);
}
}
二.CXF框架
1.下载. cxf.Apache.org
2. 服务端开发
1.创建web项目.
2.在web.xml中配置CXF框架提供的Servlet
<!-- 配置CXF框架提供的Servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
3.在类路径下提供cxf.xml核心配置文件.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
</beans>
4.开发一个接口,和实现类,实现类提供服务.
5.cxf.xml中注册服务.
<!-- 注册服务 -->
<jaxws:server id="myService" address="/customer">
<jaxws:serviceBean>
<ref bean="customerService"/>
</jaxws:serviceBean>
</jaxws:server>
其中bean指向提供服务的实现类.已经注册在Spring容器中.
3.客户端开发
1.利用Java的wsimportw命令.生成本地文件.只需要接口,和相关必须的类即可.
2.提供Spring配置文件.
3.配置客户端代理.
<!-- 注册客户端代理 -->
<jaxws:client name="myClient"
address="http://localhost:8080/crm_heima32/service/customer"
serviceClass="com.itheima.bos.utils.ICustomerService">
</jaxws:client>
address是服务所在地址.
serviceClass是接口.
4.读取配置文件,创建接口的代理对象,调用远程方法.(SSH框架中注入即可)


浙公网安备 33010602011771号