MuleSoft / Java - 错误:无法解析方法

时间:2018-03-17 06:55:25

标签: java mule runtime-error

我一直在与Mule合作开展一个简单的项目。目标是从网址中取两个数字并使用它们创建一个范围,然后我检查它是否是3,7或两者的倍数并依次打印出来。要遵循的流程代码:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:json="http://www.mulesoft.org/schema/mule/json" 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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
    <configuration doc:name="Configuration">
        <expression-language>
            <import class="sandbox.FindingMultiples" />
            <global-functions>
                def multiples(f,c)
                {
                     return sandbox.FindingMultiples.multiplesInRange(f,c);
                }
            </global-functions>
        </expression-language>
    </configuration>
    <flow name="multiples-implFlow">
        <logger level="INFO" doc:name="Log Start of Flow" message="multiples-implFlow: Starting flow..."/>
        <set-variable variableName="floor" value="#[message.inboundProperties.'http.uri.params'.floor]" doc:name="Set Floor Variable"/>
        <set-variable variableName="ceiling" value="#[message.inboundProperties.'http.uri.params'.ceiling]" doc:name="Set Ceiling Variable"/>
        <logger level="INFO" doc:name="Log Flow Variables" message="Floor: #[flowVars.floor] Ceiling: #[flowVars.ceiling]"/>
        <logger message="Starting transformation..." level="INFO" doc:name="Log Start of Transform"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
    output: multiples((flowVars.floor), (flowVars.ceiling))
}]]></dw:set-payload>
        </dw:transform-message>
        <logger message="Completing transformation. Payload: #[payload]" level="INFO" doc:name="Log Payload"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
        <logger message="Post Object to Json. Payload: #[payload]" level="INFO" doc:name="Log Payload"/>
        <logger level="INFO" doc:name="Log End of Flow" message="multiples-implFlow: Completing Flow..."/>
    </flow>
</mule>

我使用我编写的自定义Java类来处理逻辑:

package sandbox;

import java.util.*;
import java.util.stream.IntStream;

public class FindingMultiples {

    public List<String> multiplesInRange(int floor, int ceiling)
    {
        int[] range = IntStream.rangeClosed(floor, ceiling).toArray();
        List<String> result = new ArrayList<>();

        for(int i = 0; i < range.length; i++)
        {
            if(range[i] % 3 == 0)
            {
                result.add(range[i] + " : " +  "ME");
                i++;
            }else if(range[i] % 7 == 0)
            {
                result.add(range[i] + " : " +  "MAY");
                i++;
            }else if(range[i] % 3 == 0 && range[i] % 7 == 0)
            {
                result.add(range[i] + " : " + "MAY and ME");
                i++;
            }else
            {
                i++;
            }
        }

        return result;

    }

}

包构建正常,但当我尝试与Postman打电话时,我收到以下错误:

Message               : [Error: unable to resolve method: sandbox.FindingMultiples.multiplesInRange(java.lang.String, java.lang.String) [arglength=2]]
[Near : {... def multiples(f,c) ....}]
             ^
[Line: 1, Column: 1] (org.mule.mvel2.PropertyAccessException).
Payload               : {NullPayload}
Payload Type          : org.mule.transport.NullPayload
Element               : /multiples-implFlow/processors/5 @ sandbox:multiples-impl.xml:28 (Transform Message)
Element XML           : <dw:transform-message doc:name="Transform Message">
                        <dw:set-payload>%dw 1.0
%output application/java
---
{
    output: multiples((flowVars.floor), (flowVars.ceiling))
}</dw:set-payload>
                        </dw:transform-message>
--------------------------------------------------------------------------------
Root Exception stack trace:
[Error: unable to resolve method: sandbox.FindingMultiples.multiplesInRange(java.lang.String, java.lang.String) [arglength=2]]
[Near : {... def multiples(f,c) ....}]
             ^
[Line: 1, Column: 1]

我可以根据要求提供完整的堆栈跟踪。我不确定如何解决此错误。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

配置中的 <import class=**/> 语句似乎无法正确导入所需的类。

我遇到过这个问题,我通过在全局函数定义

中编写import语句来解决这个问题

你需要

  • 导入具有所需函数定义的java类。
  • 然后创建一个新对象并分配给某个变量
  • 使用“。”调用所需的方法。带有此变量的点运算符。

请尝试使用以下示例代码段

更新您的配置
<configuration doc:name="Configuration">
        <expression-language>
            <global-functions>
                def multiples(f,c)
                {
                    import sandbox.FindingMultiples;
                    fm=new FindingMultiples();
                     return fm.multiplesInRange(f,c);
                }
            </global-functions>
        </expression-language>
</configuration>