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

如何用Spring注解配置MessageDispatcherServlet与ServletRegistration?控制器未调用排查

排查SOAP服务控制器未被调用的问题

看起来你在配置Spring SOAP服务时遇到了Endpoint(控制器)不被触发的问题,结合你给出的初始化代码片段,我帮你梳理几个最常见的排查点和修复方案:

1. 补全MessageDispatcherServlet的完整配置

你代码里只写到MessageDis...,大概率是没完成Servlet的核心初始化和注册步骤。正确的配置需要包含Spring上下文关联、SOAP参数设置和URL映射,示例代码如下:

public class WebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(SoapWebServiceConfig.class); // 关联你的SOAP专属配置类

        // 初始化MessageDispatcherServlet
        MessageDispatcherServlet soapDispatcher = new MessageDispatcherServlet();
        soapDispatcher.setApplicationContext(context);
        soapDispatcher.setTransformWsdlLocations(true); // 可选,解决WSDL路径转换问题

        // 注册Servlet并绑定请求路径
        ServletRegistration.Dynamic registration = servletContext.addServlet("soapDispatcher", soapDispatcher);
        registration.setLoadOnStartup(1); // 启动时加载,避免首次请求延迟
        registration.addMapping("/soap/*"); // 确保这个路径和你的SOAP请求URL匹配
    }
}

2. 确保SOAP配置类正确注册Endpoint

你的Spring配置类必须开启SOAP服务支持,并且注册Endpoint(或通过组件扫描自动发现),示例配置类:

@Configuration
@EnableWs // 必须添加此注解开启Spring SOAP服务
@ComponentScan(basePackages = "com.yourpackage.soap.endpoints") // 扫描你的Endpoint所在包
public class SoapWebServiceConfig extends WsConfigurerAdapter {

    @Bean
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema serviceSchema) {
        DefaultWsdl11Definition wsdlDefinition = new DefaultWsdl11Definition();
        wsdlDefinition.setPortTypeName("YourServicePort");
        wsdlDefinition.setLocationUri("/soap"); // 要和Servlet映射的路径保持一致
        wsdlDefinition.setTargetNamespace("http://yourdomain.com/soap/service"); // 匹配XSD文件的命名空间
        wsdlDefinition.setSchema(serviceSchema);
        return wsdlDefinition;
    }

    @Bean
    public XsdSchema serviceSchema() {
        return new SimpleXsdSchema(new ClassPathResource("your-service-schema.xsd")); // 关联你的SOAP Schema文件
    }
}

3. 检查Endpoint类的注解和方法签名

你的SOAP控制器必须用@Endpoint标记,方法要通过@PayloadRoot指定正确的命名空间和请求体名称,否则Spring无法匹配到它:

@Endpoint
public class UserServiceEndpoint {

    private static final String NAMESPACE_URI = "http://yourdomain.com/soap/service";

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetUserRequest")
    @ResponsePayload
    public GetUserResponse handleGetUserRequest(@RequestPayload GetUserRequest request) {
        // 业务逻辑处理
        GetUserResponse response = new GetUserResponse();
        response.setUser(new User("1", "John Doe"));
        return response;
    }
}

4. 验证请求URL和SOAP消息格式

  • 请求URL必须匹配Servlet注册时的addMapping路径,比如注册的是/soap/*,那么请求地址应该是http://localhost:8080/soap/YourServicePort
  • SOAP消息的命名空间、请求体名称必须和Endpoint的@PayloadRoot参数完全一致,否则会被Spring判定为不匹配

5. 开启日志定位问题

开启Spring Web Services的DEBUG级别日志,查看是否有类似这样的日志:

DEBUG org.springframework.ws.server.MessageDispatcher - No endpoint mapping found for [SaajSoapMessage {http://wrong-namespace.com}GetUserRequest]

如果有这类日志,说明你的Endpoint未被正确注册,或者请求的命名空间/请求体名称不匹配。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 02:30:09