【问题标题】:Execution of the expression failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: String表达式执行失败。 (org.mule.api.expression.ExpressionRuntimeException)。消息有效负载的类型:字符串
【发布时间】:2015-10-05 19:24:51
【问题描述】:

我正在尝试创建一个简单的 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

不知道为什么?

【问题讨论】:

    标签: java mule


    【解决方案1】:

    您在方法 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      相关资源
      最近更新 更多