SOAP之spring与cxf集成

本文介绍SOAP协议的基本概念,并通过Spring与CXF集成的方式搭建了一个简单的WebService服务。服务端使用Spring进行管理,通过CXF提供SOAP服务;客户端通过Spring配置连接服务端。

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

一 初识SOAP

SOAP 是基于 XML 的简易协议,可使应用程序在 HTTP 之上进行信息交换。或者更简单地说:SOAP 是用于访问网络服务的协议。

  • SOAP 指简易对象访问协议
  • SOAP 是一种通信协议
  • SOAP 用于应用程序之间的通信
  • SOAP 是一种用于发送消息的格式
  • SOAP 被设计用来通过因特网进行通信
  • SOAP 独立于平台
  • SOAP 独立于语言
  • SOAP 基于 XML
  • SOAP 很简单并可扩展
  • SOAP 允许您绕过防火墙
  • SOAP 将被作为 W3C 标准来发展

 

二 构建demo

 

2.1 demo的项目结构大致如下

 

2.2 关键依赖

既然是spring与cxf的集成,这里则需要添加cxf的依赖到我们的pom文件中(spring以及其他的依赖此处省略),cxf的依赖如下:

   <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.12</version>
        </dependency>
    </dependencies>

 

2.3 server端

 

2.3.1 web.xml文件配置

由于是用cxf,所以需要在web.xml中使用CXFServlet,如下是相关配置:

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring_soapserver_basic.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置CXF的Servlet -->
    <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>/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

2.3.2 webservice接口定义及实现

这里我们可以自定义我们的服务在wsdl文件中暴露在外的请求参数,服务名,以及返回类型值等信息。

   package com.soapserver.service;

 

import com.soapserver.entity.Result;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

@WebService(targetNamespace = "selfNamespace")
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
public interface HelloSoap {
    @WebMethod(operationName = "HelloSoap")
    @WebResult(name = "response")
    Result helloSoap(@WebParam(name = "NAME") String name, @WebParam(name = "GREETING") String greeting);
}
package com.soapserver.service.impl;

import com.soapserver.entity.Result;
import com.soapserver.service.HelloSoap;
import org.springframework.stereotype.Service;

import javax.jws.WebService;

@Service("HelloSoapImpl")
@WebService(targetNamespace = "selfNamespace", serviceName = "HelloSoapService")
public class HelloSoapImpl implements HelloSoap {

    @Override
    public Result helloSoap(String name, String greeting) {
        return new Result(0, greeting + "," + name);
    }
}

2.3.3 spring配置

服务端的sping配置中需要配置我们要暴露的请求路径以及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: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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.soapserver.service">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

    <jaxws:server address="/helloSoap">
        <jaxws:serviceBean>
            <ref bean="HelloSoapImpl"/>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>

2.3.4 WSDL

待简单的demo集成后,并且发布成功,我们就可以访问我们定义好的url: http://localhost:8080/server/hellosoap?wsdl 来查看我们的webservice的wsdl文件,通过分析该文件,可以很容的出服务端的服务相关信息,并因此来定义客户端的请求。本demo的wdsl信息如下:

 

2.4 client端

2.4.1 webservice接口定义

客户端请求接口是需要分析服务端wsdl中暴露的信息来定义的。本demo的定义如下:

package com.soapclient.service;
import com.soapclient.entity.Result;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

@WebService(targetNamespace = "selfNamespace")
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
public interface HelloSoap {
    @WebMethod(operationName = "HelloSoap")
    @WebResult(name = "response")
    Result helloSoap(@WebParam(name = "NAME") String name, @WebParam(name = "GREETING") String greeting);
}

 

2.4.2 spring配置

客户端的spring配置中需要配置服务端的请求地址信息,配置如下:

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

    <jaxws:client id="request" address="http://localhost:8080/server/helloSoap"
                  serviceClass="com.soapclient.service.HelloSoap">
    </jaxws:client>
</beans>

2.4.3 main函数

本demo是用main方式打jar包来发布客户端,当然,我们也可以将我们的客户端请求集成到springmvc中,通过war包的形式来发布。

package com.soapclient.main;

import com.soapclient.entity.Result;
import com.soapclient.service.HelloSoap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring_soapclient_basic.xml");
        HelloSoap helloSoap = (HelloSoap) context.getBean("request", HelloSoap.class);
        Result result = helloSoap.helloSoap("girl", "hello");
        LOGGER.info("result: {} ; {}", result.getResult(), result.getDescription());
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值