前言
cxf的spring配置方式有很多种,而且spring在更新,cxf也一直在更新,现在spring4已经很普遍了,当然要摒弃cxf2了。 如果你要用wsdl2java生成接口类这种方式我不提倡,假如你发布的接口换个服务器部署,你连class类都要改啊!本着less code less modify的原则,以下是我的分享。
开发环境:jdk7 、tomcat7、spring4.1.1 、hibernate4 、 cxf 3.1.5 等
服务端:
1、添加cxf的jar包
我是在现有web项目的基础上增加webservice的,所以已经存在了一般web环境所需的jar包,下图红框的是我从cxf拷贝过来的新包,是我一个个测试出来的最少的必须的jar包:

>>>分享一篇很好很全面的cxf 缺包错误集锦:
http://blog.youkuaiyun.com/w1014074794/article/details/47862163
2、编写接口类和实现类
package com.yourpackage.webservice;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface YourService {
@WebMethod
public String yourMethod(yourParam...);
}
package com.yourpackage.webservice.impl;
……
import java.net.URLDecoder;
import javax.jws.WebService;
@WebService(endpointInterface = "com.yourpackage.webservice.YourService", serviceName = "YourWebService")
public class YourServiceImpl implements YourService {
@Override
public String yourMethod(yourParam...){
……
}
}
3、 整合spring发布你的接口
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="YourWebService"
class="com.yourpackage.webservice.impl.YourServiceImpl">
</bean>
<!--#yourWebService是和你的实现类上声明的serviceName一致 -->
<!--id 同上面你声明的bean id -->
<!--address 是web.xml上面配置的cxf匹配路径之后的路径,随便起命,但是访问时要一致-->
<jaxws:endpoint id="YourWebService" implementor="#YourWebService"
address="/YourService" />
</beans>
4、在web.xml文件中增加cxf支持
<!-- cxf服务-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
路径可以随意设置的,现在这样写本机服务端跑起来的时候访问wsdl的地址是:
http://localhost:8080/yourProject/ws/YourService?wsdl
这里ws之后的/YourService就是上一步配置的address值
5、运行服务端,访问wsdl
出现如下图这种xml格式的文档就说明发布成功:

至此接口已经发布完成了,关于webservice调用的方法请看下一篇:

本文介绍如何使用CXF 3.1.5结合Spring 4.1.1发布WebService。包括配置所需jar包、编写接口及实现类、整合Spring配置、配置web.xml及验证发布成功的方法。
538

被折叠的 条评论
为什么被折叠?



