如何将正文中的字段添加到条件
How to add a field from body to a condition
我用@StreamListener
注解听题。如何将 body 中的字段添加到条件中?发送到主题的实体示例:
public class Event {
private String eventId = UUID.randomUUID().toString();
private LocalDateTime eventTime = LocalDateTime.now();
private Entity entity;
}
public class Entity {
private String id = UUID.randomUUID().toString();
private String name;
}
正如您从 version 3.0 中看到的那样,您应该避免使用基于消息负载的过滤。请注意文档中的这些行:
The preceding code is perfectly valid. It compiles and deploys without any issues, yet it never produces the result you expect.
That is because you are testing something that does not yet exist in a state you expect. That is because the payload of the message is not yet converted from the wire format (byte[]) to the desired type
So, unless you use a SPeL expression that evaluates raw data (for example, the value of the first byte in the byte array), use message header-based expressions (such as condition = "headers['type']=='dog'").
因此,对于您的情况,您可以在消息中再添加一个 header 并使用 header 上的条件进行过滤,例如:
Message<Event> message = MessageBuilder.withPayload(event)
.setHeader(KafkaHeaders.MESSAGE_KEY, event.getRequestId().toString().getBytes(StandardCharsets.UTF_8))
.setHeader("entityName", event.getEnity().getName().getBytes(StandardCharsets.UTF_8))
.build();
this.streamBridge.send("binding", message);
现在你的条件是 condition = "headers['entityName']=='ABCName'"
注意:Annotation-based编程模型。基本上 @EnableBinding、@StreamListener 和所有相关的注释现在都已弃用 version 3.x
我用@StreamListener
注解听题。如何将 body 中的字段添加到条件中?发送到主题的实体示例:
public class Event {
private String eventId = UUID.randomUUID().toString();
private LocalDateTime eventTime = LocalDateTime.now();
private Entity entity;
}
public class Entity {
private String id = UUID.randomUUID().toString();
private String name;
}
正如您从 version 3.0 中看到的那样,您应该避免使用基于消息负载的过滤。请注意文档中的这些行:
The preceding code is perfectly valid. It compiles and deploys without any issues, yet it never produces the result you expect.
That is because you are testing something that does not yet exist in a state you expect. That is because the payload of the message is not yet converted from the wire format (byte[]) to the desired type
So, unless you use a SPeL expression that evaluates raw data (for example, the value of the first byte in the byte array), use message header-based expressions (such as condition = "headers['type']=='dog'").
因此,对于您的情况,您可以在消息中再添加一个 header 并使用 header 上的条件进行过滤,例如:
Message<Event> message = MessageBuilder.withPayload(event)
.setHeader(KafkaHeaders.MESSAGE_KEY, event.getRequestId().toString().getBytes(StandardCharsets.UTF_8))
.setHeader("entityName", event.getEnity().getName().getBytes(StandardCharsets.UTF_8))
.build();
this.streamBridge.send("binding", message);
现在你的条件是 condition = "headers['entityName']=='ABCName'"
注意:Annotation-based编程模型。基本上 @EnableBinding、@StreamListener 和所有相关的注释现在都已弃用 version 3.x