表达式的执行失败。 (org.mule.api.expression.ExpressionRuntimeException)。消息有效内容的类型为:String

时间:2015-10-05 10:07:17

标签: java mule

我正在尝试创建一个简单的mule应用程序,它可以读取磁盘上文件的名称和内容,并将其显示在Web浏览器上。

我的流程如下HTTP - > JAVA TRANSFORMER - 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

并且不知道为什么?

1 个答案:

答案 0 :(得分: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>