错误描述
Please retrieve the value from messageList or use getPayload(DataType.BYTE_ARRAY_DATA_TYPE)
java.lang.RuntimeException: java.lang.UnsupportedOperationException: getPayloadAsBytes(), There has been an attempt to directly access the payload of a message collection, which is unsupported.
Please retrieve the value from messageList or use getPayload(DataType.BYTE_ARRAY_DATA_TYPE)
如果是正在使用Collection Splitter
元件,并且使用不当,则会出现上述错误,解决该错误的方法是将Collection Splitter
元件放到Async
域中,Collection Splitter
将Payload
集合拆分成一个个部分,供后面的Message Processor
进一步处理,如果不放在Async
中,则Response
无法一下子处理,因此抛出UnsupportedOperationException异常
。
Flow结构图
使用 Collection Splitter 拆分数组,并且用 Logger记录拆分后的内容,最后返回一句话。如果不放在Async中,尽管仍然可以打印出正确的结果,却在最后抛出异常。
xml文档
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration1" host="0.0.0.0" port="8082" doc:name="HTTP Listener Configuration"/>
<flow name="Collection_Splitter_Test">
<http:listener config-ref="HTTP_Listener_Configuration1" path="/splitter" doc:name="HTTP"/>
<set-payload value="#[[100,101,102]]" doc:name="设置一个整形数组[100,101,102]"/>
<async doc:name="Async">
<collection-splitter doc:name="Collection Splitter"/>
<logger message="#[payload]" level="INFO" doc:name="记录经过Collection Splitter拆分后的payload"/>
</async>
<set-payload value="#['拆分成功']" doc:name="Set Payload"/>
</flow>
</mule>
参考资料
stackoverflow回答参考