You need to enable JavaScript to run this app. Spring WS(SOAP)在Java 17中的兼容性及项目构建方法咨询
优惠活动
大模型
产品
解决方案
定价
更多

Spring WS(SOAP)在Java 17中的兼容性及项目构建方法咨询

Spring WS与Java 17相关问题解答

1. Spring WS是否支持Java 17?

Spring WS 4.0及以上版本完全支持Java 17,可在Java 17环境中正常使用:

  • 旧版本(如3.x系列)最高仅兼容Java 11,无法在Java 17环境中运行;
  • Spring WS 4.x依赖的Spring Framework 6.x本身为Java 17+设计,二者适配性良好。

2. 基于Spring WS在Java 17环境下构建SOAP服务项目步骤

步骤1:添加依赖(以Maven为例)

Java 17移除了JAXB模块,需手动引入相关依赖,同时添加Spring WS核心依赖:

<dependencies>
    <!-- Spring WS核心依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
        <version>3.2.0</version> <!-- 对应Spring WS 4.x版本 -->
    </dependency>
    <!-- Java 17需手动引入JAXB相关依赖 -->
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>3.0.2</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <!-- JAXB插件:从XSD生成Java实体类 -->
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.15.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>${project.basedir}/src/main/resources/schemas</schemaDirectory>
                <generatePackage>com.example.soap.model</generatePackage>
            </configuration>
        </plugin>
    </plugins>
</build>

步骤2:定义SOAP契约(XSD Schema)

在src/main/resources/schemas目录下创建XSD文件(如customer.xsd),定义请求响应的数据结构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:tns="http://example.com/customer"
           targetNamespace="http://example.com/customer"
           elementFormDefault="qualified">

    <xs:element name="getCustomerRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="customerId" type="xs:int"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getCustomerResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="customer" type="tns:customer"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="customer">
        <xs:sequence>
            <xs:element name="id" type="xs:int"/>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="email" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

步骤3:生成Java实体类

执行Maven命令mvn clean install,插件会自动根据XSD生成对应Java类到指定包路径下。

步骤4:创建SOAP端点(Endpoint)

编写处理SOAP请求的端点类,使用Spring WS注解绑定请求:

package com.example.soap.endpoint;

import com.example.soap.model.GetCustomerRequest;
import com.example.soap.model.GetCustomerResponse;
import com.example.soap.model.Customer;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
public class CustomerEndpoint {
    private static final String NAMESPACE_URI = "http://example.com/customer";

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCustomerRequest")
    @ResponsePayload
    public GetCustomerResponse getCustomer(@RequestPayload GetCustomerRequest request) {
        GetCustomerResponse response = new GetCustomerResponse();
        Customer customer = new Customer();
        customer.setId(request.getCustomerId());
        customer.setName("John Doe");
        customer.setEmail("john@example.com");
        response.setCustomer(customer);
        return response;
    }
}

步骤5:配置Spring WS

创建配置类,配置消息调度器和WSDL定义:

package com.example.soap.config;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean<>(servlet, "/ws/*");
    }

    @Bean(name = "customers")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema customerSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CustomerPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://example.com/customer");
        wsdl11Definition.setSchema(customerSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema customerSchema() {
        return new SimpleXsdSchema(new ClassPathResource("schemas/customer.xsd"));
    }
}

步骤6:测试SOAP服务

启动项目后,可通过http://localhost:8080/ws/customers.wsdl访问WSDL文档,使用SoapUI或Postman发送以下SOAP请求验证服务:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:cus="http://example.com/customer">
   <soapenv:Header/>
   <soapenv:Body>
      <cus:getCustomerRequest>
         <cus:customerId>1</cus:customerId>
      </cus:getCustomerRequest>
   </soapenv:Body>
</soapenv:Envelope>

内容的提问来源于stack exchange,提问作者Ali Baizhanov

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 02:25:48