CXF webService 与 SpringMVC 整合要点

1.准备cxf的jar包

如果没有的话可以访问,链接:http://pan.baidu.com/s/1cCi9g6

2.在web.xml中配置cxf-servlet.xml

 <!-- CXF和servlet整合 走一下源码就知道它会找cxf-servlet.xml文件-->
  <servlet>
    <servlet-name>cxf</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <init-param>
        <param-name>config-location</param-name>
        <param-value>classpath:cxf-servlet.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
  <!-- 访问WSDL文件的时候就需要访问这个名字,通过它可以找到web服务 -->
    <servlet-name>cxf</servlet-name>
    <url-pattern>/webService/*</url-pattern>
  </servlet-mapping>

3.cxf-servlet.xml中配置 webServcie 的 spring-webService.xml的bean

<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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxws:endpoint implementor="#WeChatService" address="/weChat"/>

    <import resource="spring-webService.xml"/>

</beans>

4.spring-webService.xml注入service属性

<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean id="WeChatService" class="com.webService.WeChatService"></bean>

</beans>

5.为com.webService.WeChatService添加注解

@WebService(serviceName="WeChatService")
public class WeChatService {
     public void function1(){
     .........
     }
}

6.用web服务器,如tomcat发布访问

http://localhost:8080/ws/webService/weChat?wsdl

7.生成client文件,如

wsimport -s D:\\workspaces\\MyEclipse\\ws1Client\\src  -p com.web.client.eeams -keep http://localhost:8080/ws/webService/weChat?wsdl

特别注意:CXF在做webServices复杂对象传递时,返回值的类型不要用接口类型。例如(List 应该换成ArrayList ,Map应该换成HashMap),还有就是ArrayList<HashMap<Sting,Object>>也是cxf无法解析的,他会报list canot cast to a hashMap 建议使用 List<包装对象>来返回复杂类型。

你可能感兴趣的:(spring,Web,mvc,service,CXF)