Spring+CXF开发基于SOAP协议的WebService

本文介绍如何使用Spring和CXF框架开发SOAP协议的WebService。包括服务端接口定义、实现及配置,客户端调用流程等关键步骤。

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

spring+CXF开发SOAP协议的WebService
使用apache-cxf-2.7.18版本的cxf相关jar包

一、项目结构图——服务端:
这里写图片描述

服务接口:IWeatherInterface.java

package com.zxj.weather.ws;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface IWeatherInterface {

    public String getWeather(String cityName);
}

服务接口实现类:WeatherInterfaceImpl.java

package com.zxj.weather.ws.impl;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

import com.zxj.weather.ws.IWeatherInterface;

@Component("weatherInterfaceImpl")
@WebService
public class WeatherInterfaceImpl implements IWeatherInterface {

    @Override
    public String getWeather(String cityName) {
        System.out.println("来自于客户端:"+cityName);
        return "晴";
    }

}

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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd   
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee.xsd  
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/core 
        http://cxf.apache.org/schemas/core.xsd">

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.zxj.weather.ws" />

    <!-- 定义服务提供者 -->
    <jaxws:endpoint address="/weather" implementor="#weatherInterfaceImpl">
        <!-- 添加in拦截器 -->
        <jaxws:inInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
        </jaxws:inInterceptors>
        <!-- 添加out拦截器 -->
        <jaxws:outInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
        </jaxws:outInterceptors>
    </jaxws:endpoint>

</beans>

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>ws_cxf_spring_server</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
    <!-- Spring配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Spring监听器 -->
    <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>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/webservice/*</url-pattern>
    </servlet-mapping>
</web-app>

二、项目结构图——客户端:
这里写图片描述

1、在dos窗口中,通过wsdl2java生成客户端代码
wsdl2java -d . -p com.zxj.weather.ws.impl http://127.0.0.1:8080/ws_cxf_spring_server/webservice/weather?wsdl
备注:
-d . 代表在当前文件夹中生成代码
-p com.zxj.weather.ws.impl 代表生成代码的包名
这里写图片描述

2、生成的客户端代码列表如下:
这里写图片描述

3、调用web服务的类:WeatherClient.java

package com.zxj.weather.ws.client;

import com.zxj.weather.ws.impl.IWeatherInterface;
import com.zxj.weather.ws.impl.WeatherInterfaceImplService;

public class WeatherClient {

    public static void main(String[] args) {
        WeatherInterfaceImplService weatherInterfaceImplService = new WeatherInterfaceImplService();
        IWeatherInterface weatherInterface = weatherInterfaceImplService.getWeatherInterfaceImplPort();
        String weather = weatherInterface.getWeather("成都");
        System.out.println("成都:"+weather);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值