如果我在mule流程中有一些java或rest组件,mule就不会创建和下载文件。
<flow name="qbiif" doc:name="qbiif">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9192" doc:name="HTTP" path="QBRest"/>
<jersey:resources doc:name="REST">
<component class="com.trinet.rest.RestIntegration"/>
</jersey:resources>
<component doc:name="Java">
<singleton-object class="com.mycompany.iif.java.AppendData"/>
</component>
<set-payload value="Hello" doc:name="Set Payload"/>
<set-property propertyName="mimeType" value="text/csv" doc:name="Property"/>
<set-property propertyName="Content-Disposition" value="attachment;filename=QuickbooksAccountsJava.iif" doc:name="Property"/>
<logger message="final output==#[payload]" level="INFO" doc:name="Logger"/>
</flow>
如果我没有任何java / rest组件,那么它会下载文件,如下图所示。
<flow name="qbiif" doc:name="qbiif">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9192" doc:name="HTTP" path="QBRest"/>
<set-payload value="Hello" doc:name="Set Payload"/>
<set-property propertyName="mimeType" value="text/csv" doc:name="Property"/>
<set-property propertyName="Content-Disposition" value="attachment;filename=QuickbooksAccountsJava.iif" doc:name="Property"/>
</flow>
我需要使用java有效负载数据中的内容下载文件。
我已遵循此link,但没有更改。
修改
休息组件:
@Path("/")
public class RestIntegration {
@POST
@Path("/post/{id1}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postOperation(@PathParam("id1")String id, String cont) throws Exception{
return Response.status(200).entity(cont).build();
}
}
Java Component
public class AppendData implements Serializable {
public String appendData(@Payload String str) throws IOException, ParseException{
JSONParser jsonParser = new JSONParser();
Object jsonObjectInstance = jsonParser.parse(new StringReader(str));
StringBuilder stringBuilder = new StringBuilder();
buildHeaders(stringBuilder);
if (jsonObjectInstance instanceof JSONObject) {
JSONObject jObject = (JSONObject) jsonObjectInstance;
buildContent(stringBuilder,jObject);
}else{
JSONArray jsonArray = (JSONArray) jsonObjectInstance;
for(int i=0; i<jsonArray.size(); i++ ){
JSONObject jsonObject= (JSONObject) jsonArray.get(i);
buildContent(stringBuilder,jsonObject);
}
}
buildFooter(stringBuilder);
return (stringBuilder.toString());
}
private void buildContent(StringBuilder stringBuilder, JSONObject jsonObject) {
stringBuilder.append("ANS"+"\t");
}
private void buildHeaders(StringBuilder stringBuilder) {
stringBuilder.append("!TRNS \t Account \t Date \t\t Amount \t Description \t Memo \n");
}
private void buildFooter(StringBuilder stringBuilder) {
stringBuilder.append("ENDANNS");
}
}
如果没有Java / Rest组件,我可以下载文件。最后记录器打印正确的错误消息。
修改-2
<flow name="qbiif" doc:name="qbiif">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9192" doc:name="HTTP" path="QBRest"/>
<jersey:resources doc:name="REST">
<component class="com.trinet.rest.RestIntegration"/>
</jersey:resources>
</flow>
RestIntegration.java:
@Consumes(MediaType.APPLICATION_JSON)
@Produces("text/csv")
public Response postOperation(@PathParam("id1")String id, String cont) throws Exception{
String res = appendData(cont); // become private method
return Response.ok(res).header("Content-Disposition", "attachment;filename=QuickbooksAccountsJava.iif").header("mimeType", "text/csv").build();
我在Windows中使用Chrome Rest客户端。我得到的答复是:
答案 0 :(得分:1)
你的方法存在设计缺陷。
com.trinet.rest.RestIntegration
应该是创建预期响应的地方:
@Produces
,使其返回text/csv
,appendData
的逻辑移至postOperation
,Content-Disposition
内设置postOperation
标题。因此,除了Jersey资源之外,根本不需要任何额外的消息处理器,即你的流程应该是这样的:
<flow name="qbiif" doc:name="qbiif">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9192" doc:name="HTTP" path="QBRest"/>
<jersey:resources doc:name="REST">
<component class="com.trinet.rest.RestIntegration"/>
</jersey:resources>
</flow>