在做spring整合cxf的时候遇到了很多问题,困扰了好久,还好网上有很多资料能够帮助我解决这些问题,下面我把我的整合过程和大家分享一下,我主要在前期环境方面多说一些,因为在上面浪费了很多时间,报了很多错,不多说我就直接上图了
第一步 创建一个web工程
第二步 点击项目右键属性,Project Facets 将项目转换成web工程并选择tomcat后提交
第三步 加入自己使用jdk
第四步 使用maven导入需要的jar包
jar包地址我就不复制了,请原谅,因我都是把相关jar包都导入了,也没有简化有用的,基本spring和cxf的jar包都导入了,
第五步 创建配置文件
第1小步首先是web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/config/applicationContext-webservice.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
第2小步cxf配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<jaxws:endpoint id="ssWebService" address="/ssWebServices"
implementor="com.sshome.ssmcxf.webservice.impl.SsWebServiceImpl" />
</beans>
第3小步编写实体类 (先看下我的项目结构吧)
User类
package com.sshome.ssmcxf.entity;
public class User {
private int id;
private String userName;
private String password;
public User() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", password="
+ password + "]";
}
}
SsWebService 类
package com.sshome.ssmcxf.webservice;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface SsWebService {
public String getUser(@WebParam(name="userName") String userName,@WebParam(name="password") String password);
}
SsWebServiceImpl类
package com.sshome.ssmcxf.webservice.impl;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sshome.ssmcxf.webservice.SsWebService;
@Transactional
@Service
@WebService(endpointInterface = "com.sshome.ssmcxf.webservice.SsWebService", serviceName = "SsWebService")
public class SsWebServiceImpl implements SsWebService {
@Override
public String getUser(String userName, String password) {
return null;
}
}
最后将项目部署到tomcat上面
注意红箭头是项目名,黑箭头是web.xml配置的映射路径,灰箭头是cxf配置文件的address对应名+?wsdl是官方规定的
ok 结束