Jan*_*sen 1 java spring stomp spring-boot spring-websocket
在我使用 Spring Websocket 的 Spring Boot 1.5 应用程序中,我想在方法的返回值上设置自定义 STOMP 标头@MessageMapping,但我不知道如何执行此操作。例如:
@Controller
public class ChannelController {
@MessageMapping("/books/{id}")
public Book receive(@DestinationVariable("id") Long bookId) {
return findBook(bookId);
}
private Book findBook(Long bookId) {
return //...
}
}
Run Code Online (Sandbox Code Playgroud)
当receive从客户端触发时STOMP SEND,我希望STOMP MESSAGE带有书体的回复框架有一个自定义标题:message-type:BOOK如下所示:
MESSAGE
message-type:BOOK
destination:/topic/books/1
content-type:application/json;charset=UTF-8
subscription:sub-0
message-id:0-7
content-length:1868
{
"createdDate" : "2017-08-10T10:40:39.256",
"lastModifiedDate" : "2017-08-10T10:42:57.976",
"id" : 1,
"name" : "The big book",
"description" : null
}
^@
Run Code Online (Sandbox Code Playgroud)
如何为 a 中的回复返回值设置 STOMP 标头@MessageMapping?
如果返回值签名不重要,您可以使用SimpMessagingTemplate@Shchipunov 在他的答案的评论中指出的那样:
@Controller
@AllArgsConstructor
public class ChannelController {
private final SimpMessagingTemplate messagingTemplate;
@MessageMapping("/books/{id}")
public void receive(@DestinationVariable("id") Long bookId, SimpMessageHeaderAccessor accessor ) {
accessor.setHeader("message-type", "BOOK");
messagingTemplate.convertAndSend(
"/topic/books/" + bookId, findBook(bookId), accessor.toMap()
);
}
private Book findBook(Long bookId) {
return //...
}
}
Run Code Online (Sandbox Code Playgroud)
它正确序列化到问题中的 MESSAGE 帧。
| 归档时间: |
|
| 查看次数: |
3852 次 |
| 最近记录: |