出现该问题是因为调用了MessageHeaders类的构造函数
消息头类
MessageHeaders(Map<String, Object> headers) { } on line 39
为了不发送 id 标头,您需要调用构造函数
消息头类
MessageHeaders(Map<String, Object> headers, UUID id, Long timestamp){} on line 43
因为这个构造函数有条件不会自动创建 id header
要停止发送标头 id,您需要重写 MessageHeader 和 NotificationMessagingTemplate 类
消息头类
public class MessageHeadersCustom extends MessageHeaders {
public MessageHeadersCustom() {
super(new HashMap<String, Object>(), ID_VALUE_NONE, null);
}
}
类通知消息模板
public class NotificationMessagingTemplateCustom extends NotificationMessagingTemplate {
public NotificationMessagingTemplateCustom(AmazonSNS amazonSns) {
super(amazonSns);
}
@Override
public void sendNotification(Object message, String subject) {
MessageHeaders headersCustom = new MessageHeadersCustom();
headersCustom.put(TopicMessageChannel.NOTIFICATION_SUBJECT_HEADER, subject);
this.convertAndSend(getRequiredDefaultDestination(), message, headersCustom);
}
}
最后,将进行调用的类需要使用您的实现
package com.stackoverflow.sample.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/whatever")
public class SampleController {
@Autowired
private NotificationMessagingTemplateCustom template;
@RequestMapping(method = RequestMethod.GET)
public String handleGet() {
this.template.sendNotification("message", "subject");
return "yay";
}
}
}