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

Spring Boot SOAP WebService动态WSDL移除请求后缀问题

解决Spring Boot SOAP WSDL请求元素后缀移除问题

方案1:设置空请求后缀

你提到的Wsdl11Definition支持直接设置空字符串覆盖默认的Request后缀,无需修改请求对象类名:

@Bean
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
    DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    wsdl11Definition.setPortTypeName("CountriesPort");
    wsdl11Definition.setLocationUri("/ws");
    wsdl11Definition.setTargetNamespace(NAMESPACE_URI);
    wsdl11Definition.setSchema(countriesSchema);
    // 设置空后缀移除Request标识
    wsdl11Definition.setRequestSuffix("");
    wsdl11Definition.setResponseSuffix("Response"); // 可按需调整响应后缀
    return wsdl11Definition;
}

配置后动态生成的WSDL中,请求元素会自动从getCountryRequest变为getCountry。

方案2:用拦截器修改生成后的WSDL

如果方案1不生效,可通过拦截器在WSDL返回客户端前修改XML内容:

@Bean
public WsdlDefinitionHandlerAdapter wsdlDefinitionHandlerAdapter() {
    WsdlDefinitionHandlerAdapter adapter = new WsdlDefinitionHandlerAdapter();
    adapter.setInterceptors(new EndpointInterceptor[]{
        new EndpointInterceptor() {
            @Override
            public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
                return true;
            }

            @Override
            public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
                WebServiceMessage response = messageContext.getResponse();
                if (response instanceof SoapMessage) {
                    SoapMessage soapMessage = (SoapMessage) response;
                    Document document = soapMessage.getDocument();
                    // 批量替换带Request后缀的元素名
                    NodeList nodeList = document.getElementsByTagNameNS(NAMESPACE_URI, "*Request");
                    for (int i = 0; i < nodeList.getLength(); i++) {
                        Node node = nodeList.item(i);
                        String newNodeName = node.getLocalName().replace("Request", "");
                        document.renameNode(node, node.getNamespaceURI(), newNodeName);
                    }
                }
                return true;
            }

            @Override
            public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {
                return true;
            }

            @Override
            public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception {
            }
        }
    });
    return adapter;
}

方案3:修正XSD与注解匹配

如果之前修改XSD未生效,确认XSD中请求元素名称为无后缀格式:

<xs:element name="getCountry">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

同时保证@PayloadRoot的localPart与XSD元素名一致:

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountry")
@ResponsePayload
public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {

结合方案1的后缀设置,即可生成符合要求的WSDL。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 19:12:40