Spring-集成websocket异常"No adapter for handler SockJsHttpRequestHandler"

Spring-Integration websocket exception "No adapter for handler SockJsHttpRequestHandler"

我正在尝试创建一个基于 Spring-Integration 的 websockets 服务器。我正在使用以下配置文件(目前):

<int-websocket:server-container 
            id="serverWebSocketContainer" 
            path="websocket">
    <int-websocket:sockjs />
</int-websocket:server-container>

<int:channel id="receiveMessage"/>

<int-websocket:inbound-channel-adapter 
            channel="receiveMessage" 
            container="serverWebSocketContainer" />

<int:service-activator 
                       input-channel="receiveMessage"
                       output-channel="nullChannel"
                       ref="websocketActivator"
                       method="onReceiveMessage"
                       requires-reply="true"
                       send-timeout="60000"/>

问题是我总是遇到以下异常:

2015-03-14 01:11:01,283 DEBUG SimpleUrlHandlerMapping:  124 - Mapping [/websocket/fingervein] to HandlerExecutionChain with handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@1a767c6a] and 1 interceptor
2015-03-14 01:11:01,283 DEBUG AnnotationMethodHandlerExceptionResolver:  134 - Resolving exception from handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@1a767c6a]: javax.servlet.ServletException: No adapter for handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@1a767c6a]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
2015-03-14 01:11:01,286 DEBUG ResponseStatusExceptionResolver:  134 - Resolving exception from handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@1a767c6a]: javax.servlet.ServletException: No adapter for handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@1a767c6a]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
2015-03-14 01:11:01,287 DEBUG DefaultHandlerExceptionResolver:  134 - Resolving exception from handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@1a767c6a]: javax.servlet.ServletException: No adapter for handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@1a767c6a]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
2015-03-14 01:11:01,291 DEBUG DispatcherServlet:  989 - Could not complete request
javax.servlet.ServletException: No adapter for handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@1a767c6a]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
    at org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1147)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:72)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:186)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

知道如何解决这个问题吗?

您的 DispatcherServlet 似乎无法检测到 DispatcherServlet.properties defaultStrategies

SockJsHttpRequestHandlerHandlerAdapter 实际上是那些默认值中的第一个:

org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

或者您的应用程序上下文中有一些无法处理 HttpRequestHandler 实现的自定义 HandlerAdapter bean。在这种情况下,您还应该为 HttpRequestHandlerAdapter 添加一个 bean。

SockJsHttpRequestHandler/WebSocketHttpRequestHandlerHttpRequestHandler

的子类

所以HttpRequestHandlerAdapter可以用来做它的适配器。确保您的 DispatcherServlet 配置文件包含此类型的 bean

例如: 将以下行添加到您的 dispatch-servlet.xml(或用于您的 dispatcher-servlet 的其他 xml 文件) <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>