5分钟带你入门CXF与 SpringMVC 整合

本文指导如何在5分钟内完成CXF与SpringMVC的整合。首先,获取并引入CXF的jar包;接着,在web.xml中配置cxf-servlet.xml;然后,在cxf-servlet.xml中配置webService的spring-webService.xml的bean,并在spring-webService.xml中注入service属性;在com.webService.WeChatService上添加注解;通过Tomcat等web服务器发布并访问服务;最后,生成并使用client文件。注意,CXF处理复杂对象时,返回值类型避免使用接口,推荐使用具体类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.准备cxf的jar包

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

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<包装对象>来返回复杂类型。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值