【问题标题】:Issue converting .json file to Map将 .json 文件转换为 Map 的问题
【发布时间】:2023-03-03 19:45:01
【问题描述】:

我在将简单的 JSON 消息转换为地图时遇到问题

Example_1.json:

{"toAddress":"aaa@bbb.com",
"fromAddress":"bbb@ccc.com",
"subject":"Subject from JSON",
"body":"Body from JSON"}

弹簧配置:

<!-- Read file and output to filesIn channel -->
<int:channel id="filesIn" />
<int-file:inbound-channel-adapter id="filesReader"
                                  directory="file:/C:/data/inputDirectory"
                                  filename-pattern="*.json"
                                  channel="filesIn"
                                  prevent-duplicates="true">
    <int:poller id="poller" fixed-delay="5000" max-messages-per-poll="1" />
</int-file:inbound-channel-adapter> 

<file:file-to-string-transformer input-channel="filesIn" output-channel="strings"/>

<int:json-to-object-transformer input-channel="strings" output-channel="objectsOut" type="java.util.Map"/>

<int:object-to-map-transformer input-channel="objectsOut" output-channel="mapOut"/>

<int-file:outbound-channel-adapter channel="mapOut" directory="file:/C:/data/outputDirectory1" />


<!-- Activate logging -->

<int:channel id="filesIn">
    <int:interceptors>
        <int:wire-tap channel="LoggingChannel" />
    </int:interceptors>
</int:channel>

<int:channel id="strings">
    <int:interceptors>
        <int:wire-tap channel="LoggingChannel" />
    </int:interceptors>
</int:channel>

<int:channel id="objectsOut">
    <int:interceptors>
        <int:wire-tap channel="LoggingChannel" />
    </int:interceptors>
</int:channel>

<int:channel id="mapOut">
    <int:interceptors>
        <int:wire-tap channel="LoggingChannel" />
    </int:interceptors>
</int:channel>

<int:channel id="LoggingChannel" />
<int:service-activator input-channel="LoggingChannel" expression="@LOG.trace('Headers are: {}, Payload is: {} ', headers, payload)" />

<bean id="LOG" class="SafeLoggerFactory" factory-method="getLogger">
    <constructor-arg value="integrationEmailLogger"/>
</bean>

错误:

org.springframework.integration.handler.LoggingHandler] - org.springframework.messaging.MessageHandlingException:无法将消息有效负载写入文件;嵌套异常是 java.lang.IllegalArgumentException: 不支持的消息有效负载类型 [java.util.HashMap], failedMessage=GenericMessage [payload={subject=来自 JSON 的主题, fromAddress=bbb@ccc.com, body=来自 JSON 的正文, toAddress= aaa@bbb.com}, headers={file_originalFile=C:\data\inputDirectory\Example_1.json, id=be65b982-ef47-0207-2ec9-3fdb04837651, file_name=Example_1.json, timestamp=1575407244281}]

基本上我想将 JSON 文本转换为 Map,以便我可以使用诸如 payload.toAddress 之类的表达式使用出站通道适配器发送电子邮件

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    您应该始终包含完整的堆栈跟踪;截断的堆栈跟踪是无用的。

    为什么你有object-to-map-transformerjson-to-object-transformer 已经为您创建了地图; o-t-m-t 是多余的。

    文件int-file:outbound-channel-adapter 不知道如何处理地图负载。这就是失败的地方。

    【讨论】:

    • 嗨,加里 - 感谢您的指点!我删除了文件到字符串和对象到映射转换器以及文件出站通道适配器。然后,我能够使用来自有效载荷 &lt;int-mail:header-enricher input-channel="objectsOut" output-channel="splitMessageChannel"&gt; &lt;int-mail:to expression="payload.toAddress.toString()"/&gt; &lt;int-mail:from expression="payload.fromAddress.toString()"/&gt; &lt;int-mail:subject expression="payload.subject.toString()"/&gt; &lt;int-mail:content-type value="text/html;"/&gt; &lt;/int-mail:header-enricher&gt; 的信息来丰富标头
    猜你喜欢
    • 2014-11-19
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2015-01-10
    • 2019-05-31
    • 1970-01-01
    相关资源
    最近更新 更多