CXF安装
1.将cxf压缩包解压到任意目录
2.在环境变量中配置CXF_HOME
3.将CXF_HOME配置到Path路径 %CXF_HOME\bin
4.测试是否配置成功 wsdl2java -h
基于CXF和spring整合使用
CXF服务端配置(发布接口)
1.创建动态web项目,导入CXF相关的jar包(父项目pom.xml配置依赖架包)
2.创建接口
@WebService
public interface IHelloService {
public String sayHello(String name);
}
3.接口实现类
public class HelloServiceImpl implements IHelloService {
@Override
public String sayHello(String name) {
System.out.println("接收到客户端请求,"+name);//接口实现类,接收客户端请求
return "hello,"+name;
}
}
4.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>ws_cxf_server</display-name>
<!-- 配置spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置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>/ws/*</url-pattern> //以ws开头的请求地址 接收处理与其他请求区分开
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.配置spring容器(application.xml)
<!--
server 发布服务接口
address 服务地址 ,/服务名称
serviceClass 配置要发布服务接口全路径
serviceBean 配置服务实现类的实例
-->
<jaxws:server address="/hello2" serviceClass="cn.huitong.ws.cxf.service.IHelloService" >
<jaxws:serviceBean>
<ref bean="helloService"/><!-- 注入服务实现类 -->
</jaxws:serviceBean>
</jaxws:server>
<!-- 配置实现类对象 -->
<bean id="helloService" class="cn.huitong.ws.cxf.service.Impl.HelloServiceImpl"/>
</beans>
配置spring容器 引入标签约束
6.项目部署,测试发布
CXF客户端配置
1.创建java项目,导入CXF相关的jar包(父项目pom.xml配置依赖架包)
2.生成本地代码
CMD--->D:\Web-workspace\ws_cxf_client_cc\src\main\java>wsdl2java -d . http://localhost/ws_cxf_server/ws/hello2?wsdl
3.配置spring容器
<!--
client 配置客户端
id 相当于bean标签id
address 完整服务地址 不需要添加 ?wsdl
serviceClass 生成代码中实现类代理接口全路径
-->
<jaxws:client id="helloService" address="http://localhost/ws_cxf_server/ws/hello2" serviceClass="cn.huitong.ws.cxf.service.IHelloService"></jaxws:client>
</beans>
配置spring容器 引入标签约束
4.加载容器,获取实例
public class HelloClient {
public static void main(String[] args) {
// 使用spring工厂加载spring容器
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//根据id获取实例
IHelloService helloService = (IHelloService) context.getBean("helloService");
//调用实例方法
String result = helloService.sayHello("小法斗");
System.out.println(result);
}
}