![]() |
有胆有识的砖头 · 使用亚马逊s3的分块上传接口时内存不足问题- ...· 6 月前 · |
![]() |
坏坏的红茶 · VS2019 ...· 1 年前 · |
![]() |
追风的菠萝 · html 表格分页-掘金· 1 年前 · |
![]() |
卖萌的马铃薯 · 对外经济贸易大学学术刊物部· 1 年前 · |
![]() |
果断的猴子 · random_shuffle的使用_lizh ...· 1 年前 · |
在我使用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"));