创建Web项目
导入cxf所需要的jar包 http://cxf.apache.org/
服务端
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- 添加 CXF 的Servlet ,处理 webservice的请求 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<init-param>
<param-name>config-location</param-name>
<!-- 加载配置文件applicationContext.xml -->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
接口
package com.minimax;
import javax.jws.WebService;
@WebService
public interface HelloService {
public String query(String name);
}
接口实现类
package com.minimax;
public class HelloServiceImpl implements HelloService {
@Override
public String query(String name) {
return "你好"+name;
}
}
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="HelloService" class="com.minimax.HelloServiceImpl"></bean>
<!-- 配置cxf 地址: http://localhost:8080/Web_Cxf/service/employeeManager
组成 : http://192.168.114.10:8080 +CXF_Server( 项目名)+service(过滤的路径)+/cxfService(自定义部分)
服务类 : 服务的实现类: 拦截器 -->
<jaxws:server id="myService" address="/cxfService" serviceClass="com.minimax.HelloService">
<jaxws:serviceBean>
<ref bean="HelloService" />
</jaxws:serviceBean>
<!-- 配置输入显示日志信息的拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:server>
</beans>
浏览器访问网址 http://localhost:8080/Web_Cxf/service/cxfService?wsdl
发布成功后
cmd窗口 生成本地代理
wsimport -s F:/demo -keep http://localhost:8080/Web_Cxf/service/cxfService?wsdl
在执行后会在指定文件夹生成一些java类
将这些类放到客户端
客户端
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="myClient" serviceClass="com.minimax.HelloService"
address="http://localhost:8080/Web_Cxf/service/cxfService" />
</beans>
测试类
package com.minimax;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Ws_Client {
public static void main(String[] args) {
//读取applicationContext.xml
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService hs = (HelloService) applicationContext.getBean("myClient");
String str = hs.query("安瑾");
System.out.println(str);
}
}