如何用mule读取json文件?

时间:2022-10-29 13:37:51

I have this section in my source code:

我在源代码中有这一部分:

<file:inbound-endpoint connector-ref="fileConnector" path="C:/tmp/input" encoding="UTF-8" mimeType="application/json">
       <file:filename-wildcard-filter pattern="*.json"/>
</file:inbound-endpoint>

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

<logger level="INFO" message="#[message.payload]"/>

everything compile fine! but at execute throws the next exception: "Message: failed to transform from "json" to "java.util.HashMap" Code: MULE-ERROR-109"

一切都很好!但是在执行时抛出下一个异常:“消息:无法从”json“转换为”java.util.HashMap“代码:MULE-ERROR-109”

Can anyone help me about this error?

任何人都可以帮我解决这个错误吗?

1 个解决方案

#1


The flow was as follows:

流程如下:

<file:inbound-endpoint 
  connector-ref="fileConnector" path="C:/tmp/input" 
  doc:name="File Input" responseTimeout="10000" 
  encoding="UTF-8" mimeType="text/plain">
  <file:filename-wildcard-filter pattern="*.json"/>
</file:inbound-endpoint>

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

The error was in the JSON file. The character encoding was not right and for this reason I was generating errors when trying to transform through <json: json-to-object-transformer returnClass = "java.util.HashMap" doc: name = "JSON to Object" / > after making reading the file with <file: inbound-endpoint> attribute where I had defined as encoding = "UTF-8"

错误发生在JSON文件中。字符编码不正确,因此我在尝试通过 进行转换时产生错误使用 属性读取文件,我将其定义为encoding =“UTF-8”

Solution

Change the character encoding of the JSON file to "UTF-8"

将JSON文件的字符编码更改为“UTF-8”

Knowledge Base:

If the exception stack is:

如果异常堆栈是:

Message: Failed to transform from "json" to "java.util.HashMap" Code: MULE_ERROR-109

消息:无法从“json”转换为“java.util.HashMap”代码:MULE_ERROR-109


Exception stack is:

异常堆栈是:

  1. Unexpected character ((code 65279 / 0xFEFF) '?'): Expected to valid value (number, string, array, object, 'true', 'false' or 'null')  at [Source: java.io.InputStreamReader@2fba237; line 1, column 2 (org.codehaus.jackson.JsonParseException) org.codehaus.jackson.JsonParser: 1433 (null)
  2. 意外的字符((代码65279 / 0xFEFF)'?'):在[来源:java.io.InputStreamReader@2fba237预期有效值(数字,字符串,数组,对象,'真'','假'或'空') ;第1行第2列(org.codehaus.jackson.JsonParseException)org.codehaus.jackson.JsonParser:1433(null)

  3. Failed to transform from "json" to "java.util.HashMap (org.mule.api.transformer.TransformerException) org.mule.module.json.transformers.JsonToObject: 132 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html)
  4. 无法从“json”转换为“java.util.HashMap(org.mule.api.transformer.TransformerException)org.mule.module.json.transformers.JsonToObject:132(http://www.mulesoft.org/docs /site/current3/apidocs/org/mule/api/transformer/TransformerException.html)

Consider change the character encoding of the JSON file

请考虑更改JSON文件的字符编码

#1


The flow was as follows:

流程如下:

<file:inbound-endpoint 
  connector-ref="fileConnector" path="C:/tmp/input" 
  doc:name="File Input" responseTimeout="10000" 
  encoding="UTF-8" mimeType="text/plain">
  <file:filename-wildcard-filter pattern="*.json"/>
</file:inbound-endpoint>

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

The error was in the JSON file. The character encoding was not right and for this reason I was generating errors when trying to transform through <json: json-to-object-transformer returnClass = "java.util.HashMap" doc: name = "JSON to Object" / > after making reading the file with <file: inbound-endpoint> attribute where I had defined as encoding = "UTF-8"

错误发生在JSON文件中。字符编码不正确,因此我在尝试通过 进行转换时产生错误使用 属性读取文件,我将其定义为encoding =“UTF-8”

Solution

Change the character encoding of the JSON file to "UTF-8"

将JSON文件的字符编码更改为“UTF-8”

Knowledge Base:

If the exception stack is:

如果异常堆栈是:

Message: Failed to transform from "json" to "java.util.HashMap" Code: MULE_ERROR-109

消息:无法从“json”转换为“java.util.HashMap”代码:MULE_ERROR-109


Exception stack is:

异常堆栈是:

  1. Unexpected character ((code 65279 / 0xFEFF) '?'): Expected to valid value (number, string, array, object, 'true', 'false' or 'null')  at [Source: java.io.InputStreamReader@2fba237; line 1, column 2 (org.codehaus.jackson.JsonParseException) org.codehaus.jackson.JsonParser: 1433 (null)
  2. 意外的字符((代码65279 / 0xFEFF)'?'):在[来源:java.io.InputStreamReader@2fba237预期有效值(数字,字符串,数组,对象,'真'','假'或'空') ;第1行第2列(org.codehaus.jackson.JsonParseException)org.codehaus.jackson.JsonParser:1433(null)

  3. Failed to transform from "json" to "java.util.HashMap (org.mule.api.transformer.TransformerException) org.mule.module.json.transformers.JsonToObject: 132 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html)
  4. 无法从“json”转换为“java.util.HashMap(org.mule.api.transformer.TransformerException)org.mule.module.json.transformers.JsonToObject:132(http://www.mulesoft.org/docs /site/current3/apidocs/org/mule/api/transformer/TransformerException.html)

Consider change the character encoding of the JSON file

请考虑更改JSON文件的字符编码