在 mule ESB 上解析 json 时出错
Error parsing json on mule ESB
两天前,我的应用程序运行良好,但现在我在测试时收到错误消息,不知道问题出在哪里。知道应用程序中没有任何更改。所以我的应用程序正在侦听端口并等待 json 数据,然后获取此 json 并将其发送到将发送响应的 Web 服务。
这是我的代码片段:
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP" contentType="application/json" path="getDetails" />
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
<set-session-variable variableName="tkn" value="#[message.payload.token]" doc:name="token" />
<set-session-variable variableName="msg" value="#[message.payload.msg]" doc:name="message" />
<component doc:name="Java" class="Transformer">
<method-entry-point-resolver>
<include-entry-point method="methhod" />
</method-entry-point-resolver>
</component>
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
<logger message=""I can get properties this is app"+#[message.payload.application]+"this is the type"+#[message.payload.type]" level="INFO" doc:name="Logger" />
<http:outbound-endpoint exchange-pattern="request-response" address="http://slice.ws.com/api.php/push/send_test_push" contentType="application/x-www-form-urlencoded" doc:name="HTTP" method="POST">
<set-property propertyName="application" value="#[message.payload.application]" doc:name="Property" />
<set-property propertyName="token" value="#[sessionVars['tkn']]" doc:name="Property" />
<set-property propertyName="user" value="#[message.payload.user]" doc:name="Property" />
<set-property propertyName="type" value="#[message.payload.type]" doc:name="Property" />
<set-property propertyName="message" value="#[sessionVars['msg']]" doc:name="Property" />
</http:outbound-endpoint>
<response>
<http:response-builder status="200" doc:name="HTTP Response Builder" />
</response>
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
<logger message=""result"#[message.payload.success]" level="INFO" doc:name="Logger" />
Class
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.json.simple.*;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Transformer {
JSONObject finalObj = new JSONObject();
public JSONObject methhod(HashMap <String, String> s) {
Map <String, String> studentGrades = s;
Iterator <Map.Entry <String, String>> iterator = studentGrades.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry <String, String> studentEntry = iterator.next();
System.out.println(studentEntry.getKey() + " :: " + studentEntry.getValue());
finalObj.put(studentEntry.getKey(), studentEntry.getValue());
// iterator.remove();
}
System.out.println("json" + finalObj);
return finalObj;
}
}
错误
ERROR 2015-04-13 10:34:47,458 [[applipush].connector.http.mule.default.receiver.03] org.mule.exception.DefaultMessagingExceptionStrategy:
********************************************************************************
Message : Failed to transform from "json" to "java.lang.Object"
Code : MULE_ERROR-109
--------------------------------------------------------------------------------
Exception stack is:
1. Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.InputStreamReader@396e2b; line: 1, column: 2] (org.codehaus.jackson.JsonParseException)
org.codehaus.jackson.JsonParser:1433 (null)
2. Failed to transform from "json" to "java.lang.Object" (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)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.codehaus.jackson.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.InputStreamReader@396e2b; line: 1, column: 2]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
我正在通过邮递员发送 json 来测试应用程序:
{
"application": "91",
"token":"APA91bEkK-ghBivQRIm7Rs1HfvmXPZ4i7XpcYMA5NGpN4orsqT",
"user": "1",
"type": "Android",
"msg": "rajeun"
}
问题出在发送 json 以外数据的 Web 服务。
这是修复程序,我希望它对你有用,如果不行请告诉我。
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP" contentType="application/json" path="getDetails" />
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object" />
<set-session-variable variableName="tkn" value="#[message.payload.token]" doc:name="token" />
<set-session-variable variableName="msg" value="#[message.payload.msg]" doc:name="message" />
<component doc:name="Java" class="Transformer">
<method-entry-point-resolver>
<include-entry-point method="methhod" />
</method-entry-point-resolver>
</component>
<logger message=""I can get properties this is app"+#[message.payload.application]+"this is the type"+#[message.payload.type]" level="INFO" doc:name="Logger" />
<http:outbound-endpoint exchange-pattern="request-response" address="http://slice.ws.com/api.php/push/send_test_push" contentType="application/x-www-form-urlencoded" doc:name="HTTP" method="POST">
<set-property propertyName="application" value="#[message.payload.application]" doc:name="Property" />
<set-property propertyName="token" value="#[sessionVars['tkn']]" doc:name="Property" />
<set-property propertyName="user" value="#[message.payload.user]" doc:name="Property" />
<set-property propertyName="type" value="#[message.payload.type]" doc:name="Property" />
<set-property propertyName="message" value="#[sessionVars['msg']]" doc:name="Property" />
</http:outbound-endpoint>
<response>
<http:response-builder status="200" doc:name="HTTP Response Builder" />
</response>
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object" />
<logger message=""result"#[message.payload.success]" level="INFO" doc:name="Logger" />
<json:object-to-json-transformer returnClass="java.util.HashMap" doc:name="Object to JSON" />
两天前,我的应用程序运行良好,但现在我在测试时收到错误消息,不知道问题出在哪里。知道应用程序中没有任何更改。所以我的应用程序正在侦听端口并等待 json 数据,然后获取此 json 并将其发送到将发送响应的 Web 服务。
这是我的代码片段:
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP" contentType="application/json" path="getDetails" />
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
<set-session-variable variableName="tkn" value="#[message.payload.token]" doc:name="token" />
<set-session-variable variableName="msg" value="#[message.payload.msg]" doc:name="message" />
<component doc:name="Java" class="Transformer">
<method-entry-point-resolver>
<include-entry-point method="methhod" />
</method-entry-point-resolver>
</component>
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
<logger message=""I can get properties this is app"+#[message.payload.application]+"this is the type"+#[message.payload.type]" level="INFO" doc:name="Logger" />
<http:outbound-endpoint exchange-pattern="request-response" address="http://slice.ws.com/api.php/push/send_test_push" contentType="application/x-www-form-urlencoded" doc:name="HTTP" method="POST">
<set-property propertyName="application" value="#[message.payload.application]" doc:name="Property" />
<set-property propertyName="token" value="#[sessionVars['tkn']]" doc:name="Property" />
<set-property propertyName="user" value="#[message.payload.user]" doc:name="Property" />
<set-property propertyName="type" value="#[message.payload.type]" doc:name="Property" />
<set-property propertyName="message" value="#[sessionVars['msg']]" doc:name="Property" />
</http:outbound-endpoint>
<response>
<http:response-builder status="200" doc:name="HTTP Response Builder" />
</response>
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
<logger message=""result"#[message.payload.success]" level="INFO" doc:name="Logger" />
Class
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.json.simple.*;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Transformer {
JSONObject finalObj = new JSONObject();
public JSONObject methhod(HashMap <String, String> s) {
Map <String, String> studentGrades = s;
Iterator <Map.Entry <String, String>> iterator = studentGrades.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry <String, String> studentEntry = iterator.next();
System.out.println(studentEntry.getKey() + " :: " + studentEntry.getValue());
finalObj.put(studentEntry.getKey(), studentEntry.getValue());
// iterator.remove();
}
System.out.println("json" + finalObj);
return finalObj;
}
}
错误
ERROR 2015-04-13 10:34:47,458 [[applipush].connector.http.mule.default.receiver.03] org.mule.exception.DefaultMessagingExceptionStrategy:
********************************************************************************
Message : Failed to transform from "json" to "java.lang.Object"
Code : MULE_ERROR-109
--------------------------------------------------------------------------------
Exception stack is:
1. Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.InputStreamReader@396e2b; line: 1, column: 2] (org.codehaus.jackson.JsonParseException)
org.codehaus.jackson.JsonParser:1433 (null)
2. Failed to transform from "json" to "java.lang.Object" (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)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.codehaus.jackson.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.InputStreamReader@396e2b; line: 1, column: 2]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
我正在通过邮递员发送 json 来测试应用程序:
{
"application": "91",
"token":"APA91bEkK-ghBivQRIm7Rs1HfvmXPZ4i7XpcYMA5NGpN4orsqT",
"user": "1",
"type": "Android",
"msg": "rajeun"
}
问题出在发送 json 以外数据的 Web 服务。
这是修复程序,我希望它对你有用,如果不行请告诉我。
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP" contentType="application/json" path="getDetails" />
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object" />
<set-session-variable variableName="tkn" value="#[message.payload.token]" doc:name="token" />
<set-session-variable variableName="msg" value="#[message.payload.msg]" doc:name="message" />
<component doc:name="Java" class="Transformer">
<method-entry-point-resolver>
<include-entry-point method="methhod" />
</method-entry-point-resolver>
</component>
<logger message=""I can get properties this is app"+#[message.payload.application]+"this is the type"+#[message.payload.type]" level="INFO" doc:name="Logger" />
<http:outbound-endpoint exchange-pattern="request-response" address="http://slice.ws.com/api.php/push/send_test_push" contentType="application/x-www-form-urlencoded" doc:name="HTTP" method="POST">
<set-property propertyName="application" value="#[message.payload.application]" doc:name="Property" />
<set-property propertyName="token" value="#[sessionVars['tkn']]" doc:name="Property" />
<set-property propertyName="user" value="#[message.payload.user]" doc:name="Property" />
<set-property propertyName="type" value="#[message.payload.type]" doc:name="Property" />
<set-property propertyName="message" value="#[sessionVars['msg']]" doc:name="Property" />
</http:outbound-endpoint>
<response>
<http:response-builder status="200" doc:name="HTTP Response Builder" />
</response>
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object" />
<logger message=""result"#[message.payload.success]" level="INFO" doc:name="Logger" />
<json:object-to-json-transformer returnClass="java.util.HashMap" doc:name="Object to JSON" />