即使
@MessageMapping支持占位符,它们也不会在
@SendTo目标位置公开/解析。当前,无法使用
@SendTo注释定义动态目的地(请参阅问题SPR-12170)。您可以
SimpMessagingTemplate暂时使用(无论如何这都是内部工作方式)。这是您的处理方式:
@MessageMapping("/fleet/{fleetId}/driver/{driverId}")public void simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) { simpMessagingTemplate.convertAndSend("/topic/fleet/" + fleetId, new Simple(fleetId, driverId));}在您的代码中,将目标’ / topic / fleet / {fleetId} ‘视为文字,这就是订阅它起作用的原因,只是因为您要发送到完全相同的目标。
如果只想发送一些特定于用户的初始数据,则可以直接在订阅中返回:
@SubscribeMapping("/fleet/{fleetId}/driver/{driverId}")public Simple simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) { return new Simple(fleetId, driverId);}更新: 在Spring 4.2中,支持目标变量占位符,现在可以执行以下操作:
@MessageMapping("/fleet/{fleetId}/driver/{driverId}")@SendTo("/topic/fleet/{fleetId}")public Simple simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) { return new Simple(fleetId, driverId);}


