如何使用在POJO上应用的路由键的SPEL表达式?

时间:2017-07-20 13:55:06

标签: spring-cloud-stream spring-rabbit spring-cloud-dataflow spring-rabbitmq

我的来源APP:

@InboundChannelAdapter(value = RequestSource.CHANNEL_NAME)
public Event eventMessageSource() throws Exception {
    final Random r = new Random();
    Event event = generateSampleEvent(EventType.values()[r.nextInt(EventType.values().length - 1)]);
    System.out.println("---- Putting now ==> " + event);
    return event;
}

我的application.properties:

logging.level.=INFO
server.port=0
logging.file=events.log
server.port=0
spring.cloud.stream.bindings.requestChannel.destination=events-exchange
spring.cloud.stream.bindings.requestChannel.content-type=application/json
spring.cloud.stream.bindings.requestChannel.binder=rabbit
spring.cloud.stream.bindings.requestChannel.group=eventconsumersgroup
spring.cloud.stream.rabbit.bindings.requestChannel.producer.routing-key-expression=payload.eventType

例外是:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'eventType' cannot be found on object of type 'byte[]' - maybe not public?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:46)

我不应该使用application / json来使用SPEL吗?我在这里错过了正确的属性吗?

1 个答案:

答案 0 :(得分:1)

在评估路由键表达式之前,有效负载已转换为byte[]

您可以使用邮件标题。

@InboundChannelAdapter(value = RequestSource.CHANNEL_NAME)
public Message<Event> eventMessageSource() throws Exception {
    final Random r = new Random();
    Event event = generateSampleEvent(EventType.values()[r.nextInt(EventType.values().length - 1)]);
    System.out.println("---- Putting now ==> " + event);
    return new GenericMessage<>(event, Collections.singletonMap("type", event.getType());
}

然后表达式为headers.typeheaders['type']