2015-10-05 44 views
0

我正在嘗試創建一個簡單的mule應用程序,它讀取磁盤上文件的名稱和內容並將其顯示在Web瀏覽器上。表達式的執行失敗。 (org.mule.api.expression.ExpressionRuntimeException)。消息有效負載的類型爲:字符串

我的流程如下HTTP - > JAVA變壓器 - SETPAYLOAD - HTTP

我的Java變壓器代碼包括以下

public class ReadFile extends AbstractMessageTransformer { 

    /** 
    * loads the content of the file specified in the parameter 
    * 
    * @param filename 
    *   the name of the file 
    * @return the content of the file 
    */ 
    public String readFile(String filename) { 
     File file; 
     file = new File("O:\\test.txt"); 
     StringBuilder builder = new StringBuilder(); 
     BufferedReader reader = null; 
     try { 
      reader = new BufferedReader(new FileReader(file)); 
      String line = null; 
      while ((line = reader.readLine()) != null) 
       builder.append(line); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      closeQuietly(reader); 
     } 
     return builder.toString(); 
    } 

    public String getFileName(MuleMessage message) { 

     Path p = Paths.get("O:\\Test.txt"); 
     String file = p.getFileName().toString(); 
     return file; 

    } 

    public String setPayload(MuleMessage message, String outputEncoding) { 

     String payload1 = "#[ReadFile]"; 
     return payload1; 

    } 

    private void closeQuietly(Closeable c) { 
     if (c != null) { 
      try { 
       c.close(); 
      } catch (IOException ignored) { 
      } 
     } 
    } 

    @Override 
    public Object transformMessage(MuleMessage message, String outputEncoding) 
      throws TransformerException { 
     String filename = getFileName(message); 
     String content = readFile(filename); 
     setPayload(message, content); 
     return message; 
    } 

} 

我得到表達ReadFile的失敗的錯誤執行。 (org.mule.api.expression.ExpressionRuntimeException)。消息有效負載的類型爲:String

並且不知道爲什麼?

回答

0

你在你的方法setPayload中犯了小錯誤。那裏你沒有添加文件內容MuleMessage。不要像下面將工作

public String setPayload(MuleMessage message, String outputEncoding) { 

    message.setPayload(outputEncoding); 
    //String payload1 = "#[ReadFile]"; 
    return null; 

} 

和我的流程看起來像下面(如果我已經實現onCallable法)

<flow name="filetestFlow1"> 
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> 
    <logger message="--- Service triggred --" level="INFO" doc:name="Logger"/> 
    <component class="filetest.ReadFile" doc:name="Java"/> 
</flow> 
相關問題