在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/cpEY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录