1:步骤
(1)复制CXF的JAR包;
(2)在web.xml配置CXF的核心控制器(CXFServlet);
(3)在Spring配置文件中导入CXF的Schema和XML配置文件;
(4)在Spring配置文件中使用jaxws:endponit元素标签来暴露Web Service;
(5)如果要添加拦截器:在jaxws:endponit元素标签里添加
inInterceptors、outInterceptors子元素
2:CXF和spring整合,我准备如下jar包文件:
3:我的项目结构:
4:代码
package com.lzw.springcxf.model; public class ClubModel { private Integer clubId; private String clubName; private String country; public ClubModel() { super(); } public ClubModel(Integer clubId, String clubName, String country) { super(); this.clubId = clubId; this.clubName = clubName; this.country = country; } public Integer getClubId() { return clubId; } public void setClubId(Integer clubId) { this.clubId = clubId; } public String getClubName() { return clubName; } public void setClubName(String clubName) { this.clubName = clubName; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((clubId == null) ? 0 : clubId.hashCode()); result = prime * result + ((clubName == null) ? 0 : clubName.hashCode()); result = prime * result + ((country == null) ? 0 : country.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ClubModel other = (ClubModel) obj; if (clubId == null) { if (other.clubId != null) return false; } else if (!clubId.equals(other.clubId)) return false; if (clubName == null) { if (other.clubName != null) return false; } else if (!clubName.equals(other.clubName)) return false; if (country == null) { if (other.country != null) return false; } else if (!country.equals(other.country)) return false; return true; } }
package com.lzw.springcxf.model; public class ClubModel { private Integer clubId; private String clubName; private String country; public ClubModel() { super(); } public ClubModel(Integer clubId, String clubName, String country) { super(); this.clubId = clubId; this.clubName = clubName; this.country = country; } public Integer getClubId() { return clubId; } public void setClubId(Integer clubId) { this.clubId = clubId; } public String getClubName() { return clubName; } public void setClubName(String clubName) { this.clubName = clubName; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((clubId == null) ? 0 : clubId.hashCode()); result = prime * result + ((clubName == null) ? 0 : clubName.hashCode()); result = prime * result + ((country == null) ? 0 : country.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ClubModel other = (ClubModel) obj; if (clubId == null) { if (other.clubId != null) return false; } else if (!clubId.equals(other.clubId)) return false; if (clubName == null) { if (other.clubName != null) return false; } else if (!clubName.equals(other.clubName)) return false; if (country == null) { if (other.country != null) return false; } else if (!country.equals(other.country)) return false; return true; } }
package com.lzw.springcxf.service; import java.util.List; import com.lzw.springcxf.model.ClubModel; import com.lzw.springcxf.model.PlayerModel; public interface ClubService { public List<PlayerModel> getPlayerByClub(ClubModel Club); }
package com.lzw.springcxf.service; import java.util.List; import javax.jws.WebService; import com.lzw.springcxf.model.ClubModel; import com.lzw.springcxf.model.PlayerModel; @WebService public interface HandleService { public String sayName(String name); public List<PlayerModel> getPlayerByClub(ClubModel Club); }
package com.lzw.springcxf.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.lzw.springcxf.model.ClubModel; import com.lzw.springcxf.model.PlayerModel; import com.lzw.springcxf.service.ClubService; public class ClubServiceImpl implements ClubService { static Map<ClubModel, List<PlayerModel>> initData = new HashMap<ClubModel, List<PlayerModel>>(); static { List<PlayerModel> playA = new ArrayList<PlayerModel>(); playA.add(new PlayerModel(1, "梅西", "10", "前锋")); playA.add(new PlayerModel(2, "内马尔", "11", "前锋")); initData.put(new ClubModel(1111, "巴塞罗那", "西班牙"), playA); List<PlayerModel> playB = new ArrayList<PlayerModel>(); playB.add(new PlayerModel(3, "C罗", "7", "前锋")); playB.add(new PlayerModel(4, "贝尔", "11", "中场")); initData.put(new ClubModel(2222, "皇家马德里", "西班牙"), playB); } @Override public List<PlayerModel> getPlayerByClub(ClubModel Club) { return initData.get(Club); } }
package com.lzw.springcxf.service.impl; import java.util.List; import javax.jws.WebService; import com.lzw.springcxf.model.ClubModel; import com.lzw.springcxf.model.PlayerModel; import com.lzw.springcxf.service.ClubService; import com.lzw.springcxf.service.HandleService; @WebService(endpointInterface="com.lzw.springcxf.service.HandleService", serviceName="handleServiceTest") public class HandleServiceImpl implements HandleService { private ClubService clubService; public void setClubService(ClubService clubService) { this.clubService = clubService; } @Override public String sayName(String name) { return "本次西班牙国家德比的最佳球员是:" + name; } @Override public List<PlayerModel> getPlayerByClub(ClubModel Club) { return clubService.getPlayerByClub(Club); } }
5:配置文件
(1)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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" default-lazy-init="true"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <bean id="clubService" class="com.lzw.springcxf.service.impl.ClubServiceImpl"> </bean> <bean id="handleService" class="com.lzw.springcxf.service.impl.HandleServiceImpl"> <property name="clubService"> <ref bean="clubService"/> </property> </bean> <jaxws:endpoint implementor="#handleService" address="/cxf"> </jaxws:endpoint> </beans>(2)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>Spring-Cxf-Demo</display-name> <!--定义Spring的配置的位置,可以定义多个配置文件,可以使用通配符。 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!--设置一起动当前的Web应用,就加载Spring,让Spring管理Bean --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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>/lzw/*</url-pattern> </servlet-mapping> </web-app>