我必须从URL获取一个JSONArray,并将接收到的数据转换为一个.csv -file。我对Java和Mule比较陌生。我们使用的是Mule的社区版,所以在这里不能选择转换消息-component。
我已经创建了一个包含所有getter和setter的POJO类"JsonData“,并且我知道使用该POJO类作为Mule中的JSON- to -Object -transformer的返回类将使JSON-property名称与该POJO-class中的名称相匹配。
以下是我的JSON数据:
[
{
"accountId": "064418ca1d292a5112e9804af4dc66df5b90203c",
"iban": "FI2350009421535899",
"bic": "OKOYFIHH",
"accountName": "KÄYTTÖTILI",
"balance": 0,
"amountAvailable": 0,
"currency": "EUR"
},
{
"accountId": "07618ad83d7c5d5f2db8908d33b6a9272c5e8d96",
"iban": "FI7858400761900714",
"bic": "OKOYFIHH",
"accountName": "KASVUTUOTTO",
"balance": 3137.57,
"amountAvailable": 3137.57,
"currency": "EUR"
}
]下面是由jsonschema2pojo.org -tool生成的POJO类:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"accountId",
"iban",
"bic",
"accountName",
"balance",
"amountAvailable",
"currency"
})
public class JsonData {
@JsonProperty("accountId")
private String accountId;
@JsonProperty("iban")
private String iban;
@JsonProperty("bic")
private String bic;
@JsonProperty("accountName")
private String accountName;
@JsonProperty("balance")
private Double balance;
@JsonProperty("amountAvailable")
private Double amountAvailable;
@JsonProperty("currency")
private String currency;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("accountId")
public String getAccountId() {
return accountId;
}
@JsonProperty("accountId")
public void setAccountId(String accountId) {
this.accountId = accountId;
}
@JsonProperty("iban")
public String getIban() {
return iban;
}
@JsonProperty("iban")
public void setIban(String iban) {
this.iban = iban;
}
@JsonProperty("bic")
public String getBic() {
return bic;
}
@JsonProperty("bic")
public void setBic(String bic) {
this.bic = bic;
}
@JsonProperty("accountName")
public String getAccountName() {
return accountName;
}
@JsonProperty("accountName")
public void setAccountName(String accountName) {
this.accountName = accountName;
}
@JsonProperty("balance")
public Double getBalance() {
return balance;
}
@JsonProperty("balance")
public void setBalance(Double balance) {
this.balance = balance;
}
@JsonProperty("amountAvailable")
public Double getAmountAvailable() {
return amountAvailable;
}
@JsonProperty("amountAvailable")
public void setAmountAvailable(Double amountAvailable) {
this.amountAvailable = amountAvailable;
}
@JsonProperty("currency")
public String getCurrency() {
return currency;
}
@JsonProperty("currency")
public void setCurrency(String currency) {
this.currency = currency;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}我还有一个带有GET-request、Json- to -Object -transformer、Object-To-String -transformer、logger-component和File-endpoint的Mule流,可以将日志或有效负载写入新的文本文件。问题是,当我运行Mule流时,有效负载不是它应该的负载……下面是我的Mule流程:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:json="http://www.mulesoft.org/schema/mule/json"
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/json
http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/file
http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
<http:request-config name="HTTP_Request_Configuration" protocol="HTTPS"
host="sandbox.apis.op-palvelut.fi" port="443" doc:name="HTTP Request
Configuration"/>
<file:connector name="CreateFile" autoDelete="true" streaming="true"
validateConnections="true" doc:name="File"/>
<file:endpoint path="${juuri.csv}" name="CreateCSV" responseTimeout="10000"
doc:name="File"/>
<file:endpoint path="${juuri.log}" name="CreateLog" responseTimeout="10000"
doc:name="File"/>
<flow name="myynnittonovaFlow">
<poll doc:name="Poll">
<fixed-frequency-scheduler frequency="5" timeUnit="SECONDS"/>
<logger message="Started....." level="INFO" doc:name="Logger"/>
</poll>
<http:request config-ref="HTTP_Request_Configuration" path="v1/accounts"
method="GET" doc:name="HTTP">
<http:request-builder>
<http:header headerName="x-authorization" value="${auth}"/>
<http:header headerName="x-api-key" value="${api_key}"/>
</http:request-builder>
</http:request>
<json:json-to-object-transformer
returnClass="json.csv.testing.JsonData[]" doc:name="JSON to Object"/>
<object-to-string-transformer doc:name="Object to String"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
<file:outbound-endpoint outputPattern="log.txt" connector-
ref="CreateFile" ref="CreateLog" responseTimeout="10000"
doc:name="CreateLog"/>
</flow>
</mule>我从中得到的有效负载是:
{json.csv.testing.JsonData@22fddfa0,json.csv.testing.JsonData@34ff4054}那么,我是不是遗漏了什么,或者是什么问题呢?我希望我的解释是正确的。
更新:
我通过添加一个toString()修改了我的POJO类,现在我能够获得实际的有效负载了。我的第二个挑战是使用我创建的自定义方法将有效负载转换为CSV。
下面是我修改后的POJO-class:
public class JsonData {
@JsonProperty("accountId")
private String accountId;
@JsonProperty("iban")
private String iban;
@JsonProperty("bic")
private String bic;
@JsonProperty("accountName")
private String accountName;
@JsonProperty("balance")
private Double balance;
@JsonProperty("amountAvailable")
private Double amountAvailable;
@JsonProperty("currency")
private String currency;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String,
Object>();
@JsonProperty("accountId")
public String getAccountId() {
return accountId;
}
@JsonProperty("accountId")
public void setAccountId(String accountId) {
this.accountId = accountId;
}
@JsonProperty("iban")
public String getIban() {
return iban;
}
@JsonProperty("iban")
public void setIban(String iban) {
this.iban = iban;
}
@JsonProperty("bic")
public String getBic() {
return bic;
}
@JsonProperty("bic")
public void setBic(String bic) {
this.bic = bic;
}
@JsonProperty("accountName")
public String getAccountName() {
return accountName;
}
@JsonProperty("accountName")
public void setAccountName(String accountName) {
this.accountName = accountName;
}
@JsonProperty("balance")
public Double getBalance() {
return balance;
}
@JsonProperty("balance")
public void setBalance(Double balance) {
this.balance = balance;
}
@JsonProperty("amountAvailable")
public Double getAmountAvailable() {
return amountAvailable;
}
@JsonProperty("amountAvailable")
public void setAmountAvailable(Double amountAvailable) {
this.amountAvailable = amountAvailable;
}
@JsonProperty("currency")
public String getCurrency() {
return currency;
}
@JsonProperty("currency")
public void setCurrency(String currency) {
this.currency = currency;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public String toString() {
return "accountId: " + getAccountId() + " iban: " + getIban() + " bic: "
+ getBic() + " accountName: " + getAccountName() + " balance: " +
getBalance()
+ " amountAvailable: " + getAmountAvailable() + " currency: " +
getCurrency() + "\r\n";
}
}下面是应该转换成CSV的类……
public class DataToCSV {
public static final String HEADER =
"CODE;IBAN;BIC;NAME;BALANCE;AMOUNTAVAILABLE;CURRENCY";
public static String doCSV(Vector<JsonData> json) throws Exception {
String str = new String();
Map<String, String> values = new HashMap<String, String>();
Gson gson = new Gson();
JsonData[] data = gson.fromJson(json.toString(), JsonData[].class);
for (int i = 0; i < data.length; i++) {
try {
values.clear();
values.put("CODE", data[i].getAccountId());
values.put("IBAN", data[i].getIban());
values.put("BIC", data[i].getBic());
values.put("NAME", data[i].getAccountName());
values.put("BALANCE", Double.toString(data[i].getBalance()));
values.put("AMOUNTAVAILABLE",Double.toString(data[i].getAmountAvailable()));
values.put("CURRENCY", data[i].getCurrency());
str=str.concat(NovaUtils.doNovaLine(values,HEADER.split(";")));
} catch (Exception e) {
e.printStackTrace();
}
}
if (str != null && !str.isEmpty()) {
str = HEADER + "\r\n" + str;
}
return str;
}
}所以我实际上有两个问题;一个是doCsv()看起来是否正常,如果我想测试一下,给这个方法分配的参数是什么?
发布于 2018-12-19 23:36:38
看起来它正在做你要求它做的事情。由于您的有效负载有两个JsonData对象,因此您将获得一个由两个json对象组成的数组。
如果您想要记录实际的有效负载,可以尝试这样做:
<json:json-to-object-transformer returnClass="JsonData" doc:name="JSON to Object"/>
<foreach collection="#[payload]" doc:name="For Each">
<object-to-string-transformer doc:name="Object to String"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</foreach>您的POJO中可能需要一个toString()。
更新:我建议你访问Converting JSON to XLS/CSV in Java
https://stackoverflow.com/questions/53850275
复制相似问题