You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring Boot中使用自定义静态WSDL搭建WebService测试服务器

当然没问题!Spring WS完全支持直接暴露你自定义的静态WSDL,完美契合你不想用动态生成WSDL的需求。下面是适配Spring Boot嵌入式Tomcat、Jar包打包场景的具体实现步骤:

1. 准备静态WSDL与XSD文件

把你编写好的WSDL和对应的XSD文件放在src/main/resources/wsdl目录下(目录名可自定义,只要后续配置对应上即可)。注意WSDL里引用XSD的路径要正确,比如如果XSD和WSDL在同一目录,直接写XSD文件名即可:

<wsdl:types>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:import namespace="http://your-custom-namespace.com/service" schemaLocation="my-custom-schema.xsd"/>
    </xs:schema>
</wsdl:types>

2. 添加Spring WS依赖

在你的Maven pom.xml中引入Spring Boot的Web Services starter依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>
    <!-- 可选:如果需要XML绑定(JAXB)处理请求/响应,添加此依赖 -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>
</dependencies>

3. 配置Spring WS暴露静态WSDL

创建一个Spring配置类,通过SimpleWsdl11Definition来加载并暴露你的静态WSDL(这个类不会修改WSDL内容,完全原样返回):

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition;
import org.springframework.ws.wsdl.wsdl11.Wsdl11Definition;

@Configuration
public class WebServiceConfig {

    // Bean的名称会作为WSDL访问路径的后缀
    @Bean(name = "myCustomService")
    public Wsdl11Definition myCustomServiceWsdl() {
        SimpleWsdl11Definition wsdlDefinition = new SimpleWsdl11Definition();
        // 加载resources/wsdl下的自定义WSDL文件
        wsdlDefinition.setWsdl(new ClassPathResource("wsdl/my-custom-service.wsdl"));
        return wsdlDefinition;
    }
}

4. 配置Web服务基础路径

在application.properties里设置Spring WS的基础访问路径,方便统一管理:

# 所有Web服务相关请求(包括WSDL访问)都会基于这个路径
spring.ws.path=/ws

5. 验证访问

启动你的Spring Boot应用后,就可以通过以下地址访问到你自定义的静态WSDL了:
http://localhost:8080/ws/myCustomService.wsdl
访问后你会看到完全和你编写的一致的WSDL内容,没有任何动态生成的修改。

针对你的顾虑的说明

  • 分离XSD:静态WSDL可以自由引用外部XSD,只要路径配置正确,完全支持XSD与WSDL分离管理;
  • 输入输出命名规则:所有命名完全由你编写的WSDL和XSD定义,Spring WS不会做任何修改;
  • Maven构建阶段可用:WSDL和XSD是静态资源,Maven打包时会直接包含在Jar包中,不需要运行应用就能存在;
  • Jar包与嵌入式Tomcat:Spring Boot默认就是Jar包打包方式,嵌入式Tomcat会自动集成,无需额外配置War包。

内容的提问来源于stack exchange,提问作者Michal Krasny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:41:53