在Mule表达式语言中使用导入Java类

时间:2012-10-22 07:09:30

标签: java expression mule

我正在尝试使用通过Mule的表达式语言(MEL)导入的Java.lang.string中的.length来查找长度并在选择运算符中使用它。我认为问题是类型不匹配,但不知道如何隐藏我所拥有的,所以我能够找到它的长度。

我正在公开一个Web服务,并尝试在选择中使用端点上的POJO中的ID。我要的是payload.bookID.length> 10.因此,如果ID大于10,我可以路由到一个服务(谷歌),否则路由到UPC

目前我正在

  

执行表达式“payload.bookid.length> 10”失败。(org.mule.api.expression.ExpressionRuntimeException)。消息有效负载的类型为:BookLookupService $ Book
  造成:[错误:无法访问:长度;在类中:java.lang.String]   [近:{...未知....}]

包含配置文件的第一部分和相关的java文件。我对流程有进一步的疑问,但不知道如何发布数据映射器以便人们可以使用它们。如果有人也有这方面的提示。

谢谢!

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

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file"
    xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper"
    xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml"
    xmlns:https="http://www.mulesoft.org/schema/mule/https" xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey"
    xmlns:http="http://www.mulesoft.org/schema/mule/http" 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" version="EE-3.3.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.mulesoft.org/schema/mule/https http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd 
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd 
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd 
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/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd ">
    <mulexml:namespace-manager
        includeConfigNamespaces="true">
        <mulexml:namespace prefix="soapenv" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
        <mulexml:namespace prefix="cas" uri="http://case2.com/"/>
        <mulexml:namespace prefix="cas1" uri="http://case2.com/"/>
    </mulexml:namespace-manager>
    <data-mapper:config name="json_to_pojo"
        transformationGraphPath="json_to_pojo.grf" doc:name="DataMapper" />
    <data-mapper:config name="google_out_to_pojo" transformationGraphPath="google_out_to_pojo.grf" doc:name="google_out_to_pojo"/>
    <data-mapper:config name="google_out" transformationGraphPath="google_out.grf" doc:name="google_out"/>

    <flow name="case2Flow1" doc:name="case2Flow1">
        <http:inbound-endpoint exchange-pattern="request-response"
            address="http://localhost:8081/Case2" doc:name="HTTP"></http:inbound-endpoint>
        <cxf:simple-service serviceClass="com.case2.BookLookupService"
            doc:name="SOAP" />
        <component class="com.case2.BookLookupServiceImpl" doc:name="Java" />
        <logger
            message="Incoming payload is: #[payload]
Book ID is : #[payload.bookid]"
            level="INFO" doc:name="Logger" />
        <choice doc:name="Choice">
            <when expression="payload.bookid.length > 10">
                <flow-ref name="googleISBNFlow2" doc:name="Google" />
            </when>
        </choice>
    </flow>

</mule>

爪哇

package com.case2;

public interface BookLookupService
{
public static class BookLookup
{
    private String bookid;

    public String getBookid()
    {
        return bookid;
    }

    public void setBookid(final String bookid)
    {
        this.bookid = bookid;
    }
}

public static class Book
{
    private String bookid, name, imageurl;

    public String getBookid()
    {
        return bookid;
    }

    public void setBookid(final String bookid)
    {
        this.bookid = bookid;
    }

    public String getName()
    {
        return name;
    }

    public void setName(final String name)
    {
        this.name = name;
    }


    public String getImageURL()
    {
        return imageurl;
    }

    public void setImageURL(final String imageurl)
    {
        this.imageurl = imageurl;
    }

}

Book lookup(final BookLookup bookLookup);
}



package com.case2;

public class BookLookupServiceImpl implements BookLookupService
{
    public Book lookup(final BookLookup bookLookup)
    {
        final Book book = new Book();
        book.setName("LOTR");
        book.setBookid(bookLookup.getBookid());
        return book;
    }
}

2 个答案:

答案 0 :(得分:4)

genjosanzo在两个方面都是正确的(错误访问getBookid()和DataMapper EE支持),除了正确的MEL表达式是:

<when expression="#[message.payload.bookid.length() > 10]">

答案 1 :(得分:2)

您遇到的问题似乎与您编写when表达式的方式有关。

它应该是这样的:

<when expression="#[payload.bookid.length() > 10]">

根据数据映射器问题是EE功能,我建议您直接联系MuleSoft