我正在关注这个guide阅读 Google Pub/Sub 中某个主题的电子邮件通知。应用程序启动后,我会看到消息到达控制台,如下所示:
Message arrived! Payload: {"emailAddress":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e76767676764e69636f6762206d6163" rel="noreferrer noopener nofollow">[email protected]</a>","historyId":712315}
如何将有效负载转换为对象,以便检索 emailAddress 和 HistoryId?
服务激活器如下所示:
@Bean
@ServiceActivator(inputChannel = "pubsubInputChannel")
public MessageHandler messageReceiver() {
return message -> {
LOGGER.info("Message arrived! Payload: " + message.getPayload());
AckReplyConsumer consumer =
(AckReplyConsumer) message.getHeaders().get(GcpPubSubHeaders.ACKNOWLEDGEMENT);
consumer.ack();
};
}
我希望有效负载转换为的 POJO 对象是:
public class MessageNotification {
private String emailAddress;
private BigInteger historyId;
public String getEmailAddress() {
return emailAddress;
}
public BigInteger getHistoryId() {
return historyId;
}
public void setHistoryId(BigInteger historyId) {
this.historyId = historyId;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
最佳答案
看起来payload
是一个JSON,所以你需要去Spring Integration Reference Manual并在使用 @ServiceActivator
处理之前使用 JsonToObjectTransformer
:
@Bean
@Transformer(inputChannel = "pubsubInputChannel", outputChannel = "handleChannel")
public JsonToObjectTransformer jsonToObjectTransformer() {
return new JsonToObjectTransformer(MessageNotification.class);
}
@Bean
@ServiceActivator(inputChannel = "handleChannel")
public MessageHandler messageReceiver() {
...
关于spring-integration - Spring Integration 将有效负载转换为服务激活器中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51541576/