无法使用 Spring 引导注册 HttpRequestHandlerServlet

Cannot register HttpRequestHandlerServlet with Spring Boot

我正在尝试将 HttpRequestHandlerServlet 与 HttpRequestHandlingMessagingGateway 一起使用,以向浏览器公开简单的 REST URL。但是我无法注册 HttpRequestHandlerServlet,我是通过以下方式注册的:

@Bean
public ServletRegistrationBean inboundServletRegistration(ApplicationContext context) {
    final HttpRequestHandlerServlet servlet = new HttpRequestHandlerServlet();
    ServletRegistrationBean registration = new ServletRegistrationBean(
            servlet, "/demo/*");
    registration.setName("inboundServletRegistration");

    return registration;
}

Spring 启动应用程序启动正常,但是当尝试使用映射访问 HttpRequestHandlingMessagingGateway 端点时:

@Bean
public HttpRequestHandler httpInboundEndPoint() {
    // Http Rest gateway expecting reply.
    final HttpRequestHandlingMessagingGateway restGateway = new
            HttpRequestHandlingMessagingGateway(true);

    // Mapping of URL this gateway consumes...
    restGateway.setRequestMapping(
            mapping(new HttpMethod[]{HttpMethod.GET}, "/context/{param}"));

在地址 http://localhost:8080/demo/context/{param} 我完全崩溃了:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: 

名为 'inboundServletRegistration' 的 Bean 必须是 [org.springframework.web.HttpRequestHandler] 类型,但实际上是 [org.springframework.boot.context.embedded.ServletRegistrationBean]

类型

你遇到过这个问题吗?你能帮帮我吗?

好的,我明白了。

解决此问题的关键是在与 HttpRequestHandlerServlet 注册 bean 相同的 bean 名称下注册 HttpRequestHandler...回到 XML 配置...:-(