ule子无法将json响应转换为对象

时间:2019-02-12 10:44:30

标签: json rest mule

我无法通过Mule(3.8 CE运行时)中的JSON-to-Object组件将REST WS的JSON响应转换为对象。组件java.util.HashMap中返回的类(我也尝试过ArrayList) JSON响应

[{
        "userId": 123456,
        "otherId": 654321
    }, {
        "userId": 123457,
        "otherId": 654321
    }, {
        "userId": 123458,
        "otherId": 654321
    }
]

我收到错误

ERROR 2019-02-12 11:37:32,386 [[demorestmapper].HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Failed to transform from "json" to "java.util.HashMap"
Payload               : org.glassfish.grizzly.utils.BufferInputStream@77054f71
Transformer           : JsonToObject{this=2c88a3e8, name='JsonToHashMap', ignoreBadInput=false, returnClass=SimpleDataType{type=java.util.HashMap, mimeType='application/json', encoding='null'}, sourceTypes=[SimpleDataType{type=java.io.Reader, mimeType='*/*', encoding='null'}, SimpleDataType{type=java.net.URL, mimeType='*/*', encoding='null'}, SimpleDataType{type=java.io.File, mimeType='*/*', encoding='null'}, SimpleDataType{type=java.lang.String, mimeType='*/*', encoding='null'}, SimpleDataType{type=java.io.InputStream, mimeType='*/*', encoding='null'}, SimpleDataType{type=[B, mimeType='*/*', encoding='null'}]}
Element               : /demorestmapperFlow/processors/3 @ demorestmapper:demorestmapper.xml:18 (JSON to Object)
Element XML           : <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"></json:json-to-object-transformer>
--------------------------------------------------------------------------------
Root Exception stack trace:
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.HashMap out of START_ARRAY token
 at [Source: java.io.InputStreamReader@563690a7; line: 1, column: 1] 

1 个答案:

答案 0 :(得分:0)

这是因为它是一个JSON数组。因此,您需要在转换器中指定将其转换为地图/对象数组:

对于数组:

 <json:json-to-object-transformer returnClass="java.util.HashMap[]" />`

与地图:

<json:json-to-object-transformer returnClass="java.util.HashMap" />`