![]() |
想出家的水煮鱼 · 连博班都是落选秀,身高臂展不如他的余嘉豪能不 ...· 1 年前 · |
![]() |
不敢表白的企鹅 · DeviceHandle ...· 1 年前 · |
![]() |
忧郁的豆芽 · 如何快速实现在网页中调用文档扫描仪 ...· 2 年前 · |
![]() |
安静的自行车 · org.springframework.am ...· 2 年前 · |
![]() |
行走的消防车 · Unity基础(06)—— ...· 2 年前 · |
在我使用Spring的Spring 1.5应用程序中,我想在
@MessageMapping
方法的返回值上设置一个自定义的STOMP,但是我不知道如何做到这一点。例如:
@Controller
public class ChannelController {
@MessageMapping("/books/{id}")
public Book receive(@DestinationVariable("id") Long bookId) {
return findBook(bookId);
private Book findBook(Long bookId) {
return //...
}
当从客户端的
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
^@
如何为
@MessageMapping
中的答复返回值设置STOMP头
发布于 2017-08-10 18:25:42
如果返回值签名不重要,您可以使用
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 //...
}
它正确地序列化到问题中的消息帧。
发布于 2017-08-10 12:45:04
您可以尝试此解决方案:
@MessageMapping("/books/{id}")
public GenericMessage<Book> receive(@DestinationVariable("id") Long bookId) {
Map<String, List<String>> nativeHeaders = new HashMap<>();
nativeHeaders.put("message-type", Collections.singletonList("BOOK"));