Caused by: java.lang.IllegalStateException: No suitable default RequestUpgradeStrategy found at org.springframework.web.socket.server.support.AbstractHandshakeHandler.initRequestUpgradeStrategy(AbstractHandshakeHandler.java:145) at org.springframework.web.socket.server.support.AbstractHandshakeHandler.<init>(AbstractHandshakeHandler.java:111) at org.springframework.web.socket.server.support.DefaultHandshakeHandler.<init>(DefaultHandshakeHandler.java:35) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147) ... 55 common frames omitted
信息表明,在Spring WebSocket进行构造服务端信息处理器时,会初始化websocket服务请求策略 ,Spring已经全面内置了其所允许的策略
/** package org.springframework.web.socket.server.support; AbstractHandshakeHandler * Default constructor that auto-detects and instantiates a * {@link RequestUpgradeStrategy} suitable for the runtime container. * @throws IllegalStateException if no {@link RequestUpgradeStrategy} can be found. */ protected AbstractHandshakeHandler() { this(initRequestUpgradeStrategy()); }
//initRequestUpgradeStrategy()代码 private static RequestUpgradeStrategy initRequestUpgradeStrategy() { String className; if (tomcatWsPresent) { className = "org.springframework.web.socket.server.standard.TomcatRequestUpgradeStrategy"; } else if (jettyWsPresent) { className = "org.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy"; } else if (undertowWsPresent) { className = "org.springframework.web.socket.server.standard.UndertowRequestUpgradeStrategy"; } else if (glassfishWsPresent) { className = "org.springframework.web.socket.server.standard.GlassFishRequestUpgradeStrategy"; } else if (weblogicWsPresent) { className = "org.springframework.web.socket.server.standard.WebLogicRequestUpgradeStrategy"; } else if (websphereWsPresent) { className = "org.springframework.web.socket.server.standard.WebSphereRequestUpgradeStrategy"; } else { throw new IllegalStateException("No suitable default RequestUpgradeStrategy found"); } try { Class<?> clazz = ClassUtils.forName(className, classLoader); return (RequestUpgradeStrategy) clazz.newInstance(); } catch (Throwable ex) { throw new IllegalStateException( "Failed to instantiate RequestUpgradeStrategy: " + className, ex); } }
private static final ClassLoader classLoader = AbstractHandshakeHandler.class.getClassLoader(); private static final boolean jettyWsPresent = ClassUtils.isPresent( "org.eclipse.jetty.websocket.server.WebSocketServerFactory", classLoader); private static final boolean tomcatWsPresent = ClassUtils.isPresent( "org.apache.tomcat.websocket.server.WsHttpUpgradeHandler", classLoader); private static final boolean undertowWsPresent = ClassUtils.isPresent( "io.undertow.websockets.jsr.ServerWebSocketContainer", classLoader); private static final boolean glassfishWsPresent = ClassUtils.isPresent( "org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler", classLoader); private static final boolean weblogicWsPresent = ClassUtils.isPresent( "weblogic.websocket.tyrus.TyrusServletWriter", classLoader); private static final boolean websphereWsPresent = ClassUtils.isPresent( "com.ibm.websphere.wsoc.WsWsocServerContainer", classLoader);
public static boolean isPresent(String className, ClassLoader classLoader) { try { forName(className, classLoader); return true; } catch (Throwable ex) { // Class or one of its dependencies is not present... return false; } }
因此在未找到默认的服务策略时,会抛出
throw new IllegalStateException("No suitable default RequestUpgradeStrategy found");
本人出现问题的原因是,Tomcat应用服务器版本过低,升级版本后,fixed