0

我有这个 json 对象

data = {    
    "amount": "70",
    "cardNumber": "4222222222222",
    "expiryDate": "1215",
    "currency": "Currency.ISK.alpha" 
}

我想将此 json 对象更改为 java 组件,并使用 java 我想调用各种 java 函数。我在将此 json 对象转换为 java 组件时遇到问题。我创建了两个以下 java 类。

@JsonAutoDetect
public class Handpoint {
private String amount;
private String cardNumber;
private String expireDate;
private String currency;

public String getAmount() { return amount; }
public void setAmount(String amount) { this.amount = amount; }

public String getCardNumber() { return cardNumber; }
public void setCardNumber(String cardNumber) { this.cardNumber = cardNumber; }

public String getExpireDate() { return expireDate; }
public void setExpireDate(String expireDate) { this.expireDate = expireDate; }

public String getCurrency() { return currency; }
public void setCurrency(String currency) { this.currency = currency; }

}

public class HandpointService {
public void ProcessPerson(@Payload Handpoint handpoint) {
    String cardNumber = handpoint.getCardNumber();
    cardNumber.toString();
}
}

配置文件很简单

<flow name="json_to_java_componentFlow1" doc:name="json_to_java_componentFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081"     path="json" doc:name="HTTP"/>
    <component class="is.threesixtyfive.example.HandpointService" doc:name="Java"/>
</flow>

当我运行它时,我收到以下错误:

Message               : Failed to transform from "json" to 

"is.threesixtyfive.example.Handpoint"
Code                  : MULE_ERROR-109
--------------------------------------------------------------------------------
Exception stack is:
1. Unrecognized field "expiryDate" (Class is.threesixtyfive.example.Handpoint), not marked as ignorable
 at [Source: java.io.InputStreamReader@26940a2e; line: 1, column: 98] (through reference chain: is.threesixtyfive.example.Handpoint["expiryDate"]) (org.codehaus.jackson.map.exc.UnrecognizedPropertyException)
  org.codehaus.jackson.map.exc.UnrecognizedPropertyException:53 (null)
2. Failed to transform from "json" to "is.threesixtyfive.example.Handpoint" (org.mule.api.transformer.TransformerException)
  org.mule.module.json.transformers.JsonToObject:136 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "expiryDate" (Class is.threesixtyfive.example.Handpoint), not marked as ignorable
 at [Source: java.io.InputStreamReader@26940a2e; line: 1, column: 98] (through reference chain: is.threesixtyfive.example.Handpoint["expiryDate"])
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
    at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

任何帮助,将不胜感激

4

3 回答 3

1

JSON 和 POJO 的字段名称不同。
在 JSON 中:expiryDate
在 POJO 中:expireDate

于 2013-06-14T13:10:53.183 回答
0

问题来自以下事实:

  • 入站 HTTP 端点是request-response这样的,它会向调用者返回响应,
  • 组件方法没有返回值。

Mule 使用组件之后的任何当前消息有效负载(可能是消耗的输入流)来为入站端点创建响应并失败。

如果我添加:

<set-payload value="foo" />

在组件之后,错误就会清除(并且 HTTP 客户端会收到“foo”)。

于 2013-06-14T19:45:05.477 回答
0

我认为这样做的一种方法是首先处理转换部分,通过使用转换器 JSON 到对象并加载您的映射参考,然后使用 Java 组件并加载您的 HandpointService 类并使其成为可调用的,这样您就可以拥有一个入口点你上课。

于 2014-12-19T09:51:00.613 回答