今天尝试把一个SpringBoot项目由jdk11转移为jdk8,在IDEA里运行一切正常。打包,部署到tomcat启动后,在客户端调用WebSocket出现如下异常:
java.lang.IllegalArgumentException: javax.websocket.DeploymentException: Endpoint instance creation failed at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:139) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:942) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1732) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) Caused by: javax.websocket.DeploymentException: Endpoint instance creation failed at org.apache.tomcat.websocket.WsSession.<init>(WsSession.java:278) at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:127) ... 7 more Caused by: javax.naming.NameNotFoundException: Name [cn.aa.bb.cc/userService] is not bound in this Context. Unable to find [cn.aa.bb.cc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:840)
at org.apache.naming.NamingContext.lookup(NamingContext.java:172)
at org.apache.catalina.core.DefaultInstanceManager.lookupFieldResource(DefaultInstanceManager.java:577)
at org.apache.catalina.core.DefaultInstanceManager.processAnnotations(DefaultInstanceManager.java:480)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:173)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:142)
at org.apache.tomcat.websocket.WsSession.<init>(WsSession.java:275)
... 8 more
在网上搜索了一阵也没有找到解决办法。回看异常本身,cn.aa.bb.cc/userService 有点问题,cn.aa.bb.cc是Controller类,userService 是类成员变量。通过 @Resource 初始化userService。从异常来看是由于初始化失败导致的。
@Resource private UserService userService;
将Resource改为Autowired后,再部署运行就没问题了。
@Autowired private UserService userService;
看来是找不到合适的类来初始化userService啊。