WebSocket @ServerEndpoint 注解路径参数传递
在 WebSocket 的 @ServerEndpoint 注解中,可以使用 @PathParam 注解来传递参数在路径中。例如:
@ServerEndpoint("/websocket/{userId}")
public class MyWebSocket {
@OnOpen
public void onOpen(Session session, @PathParam('userId') String userId) {
// 在路径中获取 userId 参数
}
// ...
}
在上面的例子中,WebSocket 的路径为 '/websocket/{userId}',其中的 '{userId}' 代表一个动态参数。当客户端连接到 WebSocket 时,可以在路径中携带 userId 参数,例如 '/websocket/123',这样在 @OnOpen 方法中可以通过 @PathParam('userId') 注解来获取该参数的值。
原文地址: https://www.cveoy.top/t/topic/nvbr 著作权归作者所有。请勿转载和采集!