SpringMVC架构新增Soap风格的Webservice服务(CXF组件实现)

本文介绍如何使用Apache CXF框架搭建SOAP Web服务,包括配置环境、服务端及客户端的具体步骤,并提供关键代码示例。

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


配置环境:

  jdk1.6+Tomacat7.0.35+Spring4.14+cxf3.0.13

    (注:配置相应高版本的cxf组件包需要更高一级的jdk实现)

接口服务端配置

1.引入cxf的相关依赖jar包

       从Apache官网中下载的cxf包的lib中获取如下jar:
        

2.Web.xml中配置CXFServlet

       假设SpringMVC本身的DispatcherServlet已经启用,则在第2启动顺序添加CXFServlet。并添加servlet-mapping匹配请求。
配置如下:
    <!-- Apache CXFServlet -->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXF Servlet</display-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
        <init-param>
	      <param-name>config-location</param-name>
	      <param-value>classpath:/webservice.xml</param-value>
	    </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <!-- CXFServlet Mapping -->
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

3.配置自定义webservice.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:p="http://www.springframework.org/schema/p"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.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">
        
    <!-- cxf必要配置 -->
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
      
    <bean id="surveyService" class="com.recuitment.ws.cxf.impl.SurveyService">
        <property name="excludeName" value="Michael"/>
        <property name="leastPonit" value="10"/>
    </bean>
    
    <!-- Expose SurveyWebService -->
    <jaxws:server id="surveyWebService" 
        serviceClass="com.recuitment.ws.cxf.ISurveyService" 
        address="/surveyWebService">
        <jaxws:serviceBean>			
            <ref bean="surveyService"/>
        </jaxws:serviceBean>
    </jaxws:server>

</beans>

4.编写相应接口及实现类

package com.recuitment.ws.cxf;

import javax.jws.WebService;

@WebService
public interface ISurveyService
{
    /**
     * @param username 名字
     * @param point 分数
     * @return
     */
    public String vote(String username,int point);
}

5.查看相关服务发布情况

启动SpringMVC项目,根据配置文件定义,接口地址类似:http://ip:port/项目名/ws/**

接口客户端配置

1.引入cxf的相关依赖jar包

       从Apache官网中下载的cxf包的lib中获取如下jar(其中cxf.jar为服务端打包的jar):

2.增加客户端的xml文件和java文件,如下:

<?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-2.5.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd">
        
        <!-- 导入Apache CXF Bean -->
      <import resource="classpath:META-INF/cxf/cxf.xml"/>
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
        
      <jaxws:endpoint id="surveyService"
           implementor="com.recuitment.ws.cxf.ISurveyService"
        address="http://localhost:8080/recuitment/SurveyWebService"/>
        
        
</beans>
package com.recuitment.cxf;


import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;


import com.recuitment.ws.cxf.ISurveyService;




public class SurveyServiceClient
{
    
    /** 
     * <一句话功能简述>
     * <功能详细描述>
     * @param argsO
     * @see [类、类#方法、类#成员]
     */
 public static void main(String[] args)
    {
        // TODO Auto-generated method stub
     System.out.println(SurveyServiceClient.doSpringClient("Michael",9));


    }
   private static String doSpringClient(String username,int point){
       


        ApplicationContext context = new FileSystemXmlApplicationContext("/resource/beanRefClient.xml");
        ISurveyService surveyService = (ISurveyService)context.getBean("surveyServiceClient");
        
        return surveyService.vote(username,point);
        
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值