Our javascript websocket clients adds "custom" headers to all STOMP
messages.
My project handles websocket endpoints using spring-websocket @Controller
.
@MessageMapping(value = "/mymessages")
public void save(@Payload ToBeSaved payload, @Headers MessageHeaders headers) {
service.save(toMsg(payload, headers));
}
protected <P> Message<P> toMsg(P payload, MessageHeaders headers) {
return MessageBuilder.createMessage(payload, headers);
}
The controller modifies the payload and then passes the new payload and original websocket headers (including the custom ones) to a spring-integration @MessagingGateway
.
The underlying IntegrationFlow
tries to access the "custom" headers by accessing the message headers with the SPLExpression headers['custom']
.
Unfortunately headers['custom']
is always null because custom
is actually contained in the nativeHeaders
.
I haven't found a way to tell IntegrationFlow
to look into nativeHeaders
.
Is there a way in spring-websocket to copy all native headers as normal headers ?
Thanks in advance
答案 0 :(得分:2)
spring-websocket对你来说无能为力。这不是它的责任。
如果您真的想要访问nativeHeaders
中的某些内容,则应手动执行此操作。
对于您的特定情况,SpEL可能如下所示:
headers['nativeHeaders']['custom']
因为nativeHeaders
也是Map
。
从另一方面,您可以在下游流程中使用<header-enricher>
将所有nativeHeaders
弹出到顶级。
还有一点:自Spring Integration 4.2
以来,我们为STOMP适配器提供原生支持。并且有一个StompHeaderMapper
可以完全按照您的要求执行,代码如下:
else if (StompHeaderAccessor.NATIVE_HEADERS.equals(name)) {
MultiValueMap<String, String> multiValueMap =
headers.get(StompHeaderAccessor.NATIVE_HEADERS, MultiValueMap.class);
for (Map.Entry<String, List<String>> entry1 : multiValueMap.entrySet()) {
name = entry1.getKey();
if (shouldMapHeader(name, this.outboundHeaderNames)) {
String value = entry1.getValue().get(0);
if (StringUtils.hasText(value)) {
setStompHeader(target, name, value);
}
}
}
}