如何通过Spring集成正确获取入站消息头 [英] how to correctly get inbound message headers with spring integration
问题描述
我将在春季收到一封电子邮件,其中包含一个非常基本的脚本
I'm receiving email with spring, with a very basic script so far
ApplicationContext ac = new ClassPathXmlApplicationContext("imap.xml");
DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
inputChannel.subscribe(message -> {
System.out.println(message.getHeaders());
System.out.println(message.getPayload());
MessageHeaders headers = message.getHeaders();
String from = (String) headers.get("mail_from");
});
根据文档,我认为标头会被自动解析,但是我在第一个System.out.println();
中获得的标头只是
According to the documentation I thought the headers would get parsed automatically, but the headers I get with the first System.out.println();
are just
{id=c65f55aa-c611-71ee-c56d-6bf13c6f71d0, timestamp=1468869891279}
第二个输出(对于getPayload()
)是
org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@6af5615d
from
输出null
...
然后我尝试使用MailToStringTransformer
MailToStringTransformer a = transformer();
a.setCharset("utf-8");
System.out.println(a.transform(message));
哪个输出有效负载和我期望的所有标头,但是(自然地)输出为String.
Which outputs the payload and all the headers I have expected, but (naturally) as a String.
我该怎么做才能在对象中获取消息标题和文本?
What do I have to do to get the messages headers and text in an object?
推荐答案
不确定您要引用的文档是什么,但是当前的4.3
版本具有以下内容:
Not sure which documentation are you referring, but the current 4.3
version has this:
默认情况下,入站适配器生成的消息的有效负载是原始的
MimeMessage
;您可以使用该对象询问标题和内容.从版本4.3开始,您可以提供一个HeaderMapper<MimeMessage>
来将标头映射到MessageHeaders
.为了方便起见,为此提供了DefaultMailHeaderMapper
.
By default, the payload of messages produced by the inbound adapters is the raw
MimeMessage
; you can interrogate the headers and content using that object. Starting with version 4.3, you can provide aHeaderMapper<MimeMessage>
to map the headers toMessageHeaders
; for convenience, aDefaultMailHeaderMapper
is provided for this purpose.
还有一点:
当不提供标题映射器时,消息有效负载是javax.mail呈现的MimeMessage.该框架提供了
MailToStringTransformer
...
如果需要对映射进行一些自定义,则始终可以提供自己的HeaderMapper<MimeMessage>
实现或DefaultMailHeaderMapper
扩展名.
If you need some customization on the mapping you always can provide your own HeaderMapper<MimeMessage>
implementation or DefaultMailHeaderMapper
extension.
这篇关于如何通过Spring集成正确获取入站消息头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!