Spring WebService的两种主流实现方式‌及说明

 更新时间:2026年04月27日 11:11:46   作者:曹牧  
文章介绍了Spring Web Services(Spring-WS)和Spring Boot + Apache CXF两种主流的Web服务实现方式,Spring-WS采用ContractFirst方式,适合企业级、高可维护性的SOAP服务;Spring Boot + CXF采用ContractLast方式,开发更快,适合快速原型或内部系统集成

Spring WebService主流实现方式‌

Spring-WS(Spring Web Services)‌:采用 ‌Contract First(自顶向下)‌ 方式,先定义 XSD/WSDL,再生成 Java 代码。适用于企业级、高可维护性的 SOAP 服务。

Spring Boot + JAX-WS(通常用 Apache CXF)‌:采用 ‌Contract Last(自底向上)‌ 方式,通过 @WebService 注解将 Java 类暴露为 Web Service。开发更快速,适合快速原型或内部系统集成。

1、‌Spring-WS(Contract First)核心步骤‌

添加依赖‌(Maven):

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>
    <dependency>
        <groupId>wsdl4j</groupId>
        <artifactId>wsdl4j</artifactId>
    </dependency>

定义 XSD Schema‌(如 src/main/resources/xsd/login.xsd):

<xs:schema targetNamespace="http://example.com/ws/login"
               elementFormDefault="qualified"
               xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="loginRequest">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="username" type="xs:string"/>
                    <xs:element name="password" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="loginResponse">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="result" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>

自动生成 Java 类‌(使用 JAXB2 Maven 插件):

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>1.6</version>
        <configuration>
            <schemaDirectory>${project.basedir}/src/main/resources/xsd/</schemaDirectory>
            <outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
        </configuration>
        <executions>
            <execution>
                <goals><goal>xjc</goal></goals>
            </execution>
        </executions>
    </plugin>

创建 Endpoint‌:

@Endpoint
    public class LoginEndpoint {
        private static final String NAMESPACE_URI = "http://example.com/ws/login";
        @PayloadRoot(namespace = NAMESPACE_URI, localPart = "loginRequest")
        @ResponsePayload
        public LoginResponse login(@RequestPayload LoginRequest request) {
            // 业务逻辑
            LoginResponse response = new LoginResponse();
            response.setResult("success");
            return response;
        }
    }

配置 WebServiceConfig‌:

    @Configuration
    @EnableWs
    public class WebServiceConfig extends WsConfigurerAdapter {
        @Bean
        public ServletRegistrationBean messageDispatcherServlet(ApplicationContext context) {
            MessageDispatcherServlet servlet = new MessageDispatcherServlet();
            servlet.setApplicationContext(context);
            servlet.setTransformWsdlLocations(true);
            return new ServletRegistrationBean(servlet, "/ws/*");
        }
        @Bean(name = "login")
        public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema) {
            DefaultWsdl11Definition wsdl = new DefaultWsdl11Definition();
            wsdl.setPortTypeName("loginPort");
            wsdl.setLocationUri("/ws");
            wsdl.setSchema(schema);
            return wsdl;
        }
        @Bean
        public XsdSchema schema() {
            return new SimpleXsdSchema(new ClassPathResource("xsd/login.xsd"));
        }
    }

访问 WSDL‌:

启动应用后,访问 http://localhost:8080/ws/login.wsdl 查看服务描述。

2、‌Spring Boot + CXF(Contract Last)

添加依赖‌:

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
        <version>3.3.1</version>
    </dependency>

定义接口与实现类‌:

    @WebService
    public interface OrderWS {
        @WebMethod
        Order getOrderById(int id);
    }
    @WebService(endpointInterface = "com.example.OrderWS")
    public class OrderWSImpl implements OrderWS {
        @Override
        public Order getOrderById(int id) {
            return new Order(id, "Product", 999.99);
        }
    }

配置发布端点‌:

    @Configuration
    public class WebServiceConfig {
        @Autowired
        private OrderWSImpl orderWSImpl;
        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint = new EndpointImpl(new SpringBus(), orderWSImpl);
            endpoint.publish("/orderws");
            return endpoint;
        }
    }

访问 WSDL‌:

http://localhost:8080/orderws?wsdl

3、‌注意事项‌

  • Spring-WS‌ 更适合需要严格契约控制、长期维护的系统。
  • CXF + JAX-WS‌ 开发更快,但耦合度较高,适合内部服务或快速迭代场景。
  • 避免使用过时的 Axis1/Axis2,除非维护遗留系统 ‌。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • MyBatis-Plus动态返回实体类示例详解

    MyBatis-Plus动态返回实体类示例详解

    这篇文章主要为大家介绍了MyBatis-Plus动态返回实体类示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Android中的LinearLayout布局

    Android中的LinearLayout布局

    在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了,线性布局是按照垂直方向(vertical)或水平方向(horizontal)的顺序依次排序子元素,每一个子元素都位于前一个元素之后,下面我们就简单的了解一下吧
    2017-01-01
  • java中gc算法实例用法

    java中gc算法实例用法

    在本篇文章里小编给大家整理了一篇关于java中gc算法实例用法,有兴趣的朋友们可以参考学习下。
    2021-01-01
  • 在Spring中使用JDBC和JDBC模板的讲解

    在Spring中使用JDBC和JDBC模板的讲解

    今天小编就为大家分享一篇关于在Spring中使用JDBC和JDBC模板的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • SpringBoot+Mybatis-plus+shardingsphere实现分库分表的方案

    SpringBoot+Mybatis-plus+shardingsphere实现分库分表的方案

    实现亿级数据量分库分表的项目是一个挑战性很高的任务,下面是一个基于Spring Boot的简单实现方案,感兴趣的朋友一起看看吧
    2024-03-03
  • 使用springmvc的controller层获取到请求的数据方式

    使用springmvc的controller层获取到请求的数据方式

    这篇文章主要介绍了使用springmvc的controller层获取到请求的数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java实现最小高度树

    Java实现最小高度树

    本文主要介绍了Java实现最小高度树,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • MVC+DAO设计模式下的设计流程详解

    MVC+DAO设计模式下的设计流程详解

    这篇文章主要介绍了MVC+DAO设计模式下的设计流程详解,分别介绍了数据库设计、设计符合java bean标准的entity类、设计访问数据库的DAO接口等内容,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • SpringBoot接收前端参数的最常用的场景和具体案例

    SpringBoot接收前端参数的最常用的场景和具体案例

    在SpringBoot开发中接收参数是非常常见且重要的一部分,依赖于请求的不同场景,Spring Boot提供了多种方式来处理和接收参数,项目小编就和大家简单讲讲SpringBoot接收前端参数的3 个最常用的场景和具体案例
    2025-11-11
  • 一段代码搞懂关于Java中List、Set集合及Map的使用

    一段代码搞懂关于Java中List、Set集合及Map的使用

    这篇文章主要介绍了关于Java中List、Set集合及Map的使用及list,set和map三者的区别介绍,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-08-08

最新评论