搜了很多,发现有几种错误及解决办法,有类似可对比参照:
1、路径错误
问题:路径前面没有“/”,会导致这个错误。
错误代码:
@ServerEndpoint("imserver/{token}")
正确代码:
@ServerEndpoint("/imserver/{token}")
2、@PathParam的使用
问题:获取路径中的参数要使用@PathParam,若使用了其他的会报错。
@OnOpen
public void openConnection(Session session, @PathParam("token") String token)
3、@Component注解问题
问题:去掉类名上的@Component注解可解决报错,亲测确实没有报错了,但是在本地也无法建立连接了,只能打包时侯将该注解去掉,有用但不太方便。
4、AOP问题
后面那一串是代表类被CGLIB转换为了代理对象,WebSocketServer这个类被切了,以至于@ServerEndPoint注解无法注入至对应的对象,导致报错;检查自己项目中的AOP切入点,包括springboot配置文件里的,排除掉 WebSocketServer不被切就可以了。
5、@OnError注解下方法的问题(本人项目解决方法)
大致意思是: 类型为[class java.lang]的参数。在类[java.lang.reflect]的[onError]方法上发现异常。方法],它没有@PathParam注释
解决方法:检查onError方法,发现没有Throwable参数,加上之后就好了,加完后的代码如下:
@OnError
public void onError(Throwable throwable){
logger.error("连接错误,原因:"+throwable.getMessage());
}
还有一种情况下也可以使用这种解决方法:
Caused by: javax.websocket.DeploymentException: No Throwable parameter was present on the method [onError] of class [com.dxhy.bxsdk.business.websocketService.MessageWebsocket] that was annotated with OnError
意思是:javax.websocket.DeploymentException:使用 OnError 注释的类 [com.dxhy.bxsdk.business.websocketService.MessageWebsocket] 的方法 [onError] 上没有 Throwable 参数