Spring WebSockets @SubscribeMapping 中的 PathVariables 不起作用

Posted

技术标签:

【中文标题】Spring WebSockets @SubscribeMapping 中的 PathVariables 不起作用【英文标题】:PathVariables in Spring WebSockets @SubscribeMapping not working 【发布时间】:2015-05-08 00:32:18 【问题描述】:

我正在尝试类似于“Path variables in Spring WebSockets @SendTo mapping”的东西 但我想将表名作为附加信息发送到 @SubscribeMapping("/topic/data")。 “tablename”可以是任何基于我的需要(我想设置的),它应该连接 @SubscribeMapping("/topic/data/tablename") 并且在服务器端,我会喜欢访问表名以从数据库中获取数据。我已经尝试了上述帖子留置权@DestinationVariable 中提到的解决方案,但我认为我遗漏了一些东西。

【问题讨论】:

我终于弄明白了。需要在服务器端添加类似的细节 【参考方案1】:

在服务器端:

 @SubscribeMapping("/getviewschema/tablename")
 public JSONObject getViewSchema(@DestinationVariable String tablename) throws Exception
 
     DataManager manager = new DataManager();
     return manager.getViewJSONSchema(tablename);
 

在客户端

socket.stomp.subscribe("/app/getviewschema/"+service.tablename,function(data) 

        listenerview.notify(JSON.parse(data.body));
);  

【讨论】:

感谢您的回答,它确实有效。我在文档中错过了它,它在这里:docs.spring.io/spring/docs/5.0.x/spring-framework-reference/…

以上是关于Spring WebSockets @SubscribeMapping 中的 PathVariables 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

使用 spring 启用 WebSockets

对如何使用 Spring-websockets 进行 stomp 调用感到困惑

Spring 4 中 Websockets 的自定义对象映射器

Spring消息传递+安全如何通过websockets登录?

Spring-Websockets 4.2 中使用 SockJS 的部分消息

Spring 4 STOMP Websockets Heartbeat