1、新建一个web项目
2、导入cxf相应的jar包,并部署到项目中
3、服务接口
package com.xiaostudy;
/**
* @desc 服务器接口
* @author xiaostudy
*
*/
public interface Test_service {
public String getNumber(String number);
}
4、服务接口实现类
package com.xiaostudy;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
/**
* @desc 服务器接口实现类
* @author xiaostudy
*
*/
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)//SOAP1.2声明
public class Test_serviceImpl implements Test_service {
/**
* @desc 处理客户端的数据,并返回数据
* @param number 参数
* @return String 返回数据类型
*/
@Override
public String getNumber(String number) {
System.out.println("我服务器端执行了。。。。");
return number+"_xiaostudy";
}
}
5、编写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:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- <jaxws:server发布SOAP协议的服务 ,对JaxWsServerFactoryBean类封装-->
<jaxws:server address="/number" serviceClass="com.xiaostudy.Test_serviceImpl">
<jaxws:serviceBean>
<ref bean="test_serviceImpl"/>
</jaxws:serviceBean>
<!-- 配置拦截器 -->
<jaxws:inInterceptors>
<ref bean="inInterceptor"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outInterceptor"/>
</jaxws:outInterceptors>
</jaxws:server>
<!-- 配置服务实现类 -->
<bean name="test_serviceImpl" class="com.xiaostudy.Test_serviceImpl"/>
<!-- 配置拦截器的bean -->
<bean name="inInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean name="outInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
</beans>
6、编写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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ws_2_cxf_spring_server</display-name>
<!-- 设置spring的环境 -->
<context-param>
<!--contextConfigLocation是不能修改的 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置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>
</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>
7、Tomcat服务开启