一.apache CXF简介:
Apache CXF是开源的,CXF是两个项目的结合:由IONA技术公司(现在是Progress的一部分)开发的Celtix和由Codehaus主持的团队开发的XFire,合并是由人们在Apache软件基金会共同完成的。CXF的名字来源于"Celtix"和"XFire"的首字母。是WebService的一个框架.
二.案例:服务器端开发
1.创建web项目,导入cxf必jar须包https://pan.baidu.com/s/1cPC6KbO_DE25oZIWEy9JUg
2.在web.xml中配置CXF框架提供的一个Servlet:
<!-- 配置CXF框架提供的Servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 通过初始化参数指定CXF框架的配置文件位置 -->
<init-param>
<param-name>config-location</param-name>
<param-value>classpath:cxf.xml</param-value>
</init-param>
</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" /><bean id="helloService" class="com.wenhao.service.SayHelloImpl"/>
<!-- 注册服务 -->
<jaxws:server id="myService" address="/cxfService">
<jaxws:serviceBean>
<ref bean="helloService"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
4.开发一个接口和实现类
@WebService
public interface SayHello {
public String sayWorld(String name);
}
public class SayHelloImpl implements SayHello {
@Override
public String sayWorld(String name) {
System.out.println("cxf框架服务端调用了!!!!");
return "你好" + name;
}
}
5.在cxf.xml中注册服务
6.运行,发布服务端代码
在浏览器中输入 http://localhost/CXFService/service/cxfService?wsdl 生成wsdl以供客户端调用
三:客户端开发
1.使用jdk提供的wsimport命令生成本地代码完成调用
在cmd窗口下输入
生成的接口文件将会在当前目录下,
2.将接口文件复制到项目中
将生成的接口文件复制到项目中,并在applicationContext.xml文件中注册服务
<!-- 注册服务 -->
<jaxws:client id="myClient" address="http://localhost/CXFService/service/cxfService"
serviceClass="com.wenhao.service.SayHello">
</jaxws:client>
测试调用