调用HTTP端点后,如何在Mule Anypoint Studio中读取XML文件?

调用HTTP端点后,如何在Mule Anypoint Studio中读取XML文件?,mule,mule-studio,mule-el,Mule,Mule Studio,Mule El,我是Anypoint Studio的新手,我正在尝试创建一个具有HTTP连接器和端点的流,当用户调用HTTP端点时,应用程序将加载XML文件的内容,然后将转换为JSON,并在HTTP响应中返回JSON 我的流配置如下所示 <flow name="test-flow"> <http:listener config-ref="HttpListenerConfiguration" path="/read" allowedMethods="POST" doc:name=

我是Anypoint Studio的新手,我正在尝试创建一个具有HTTP连接器和端点的流,当用户调用HTTP端点时,应用程序将加载XML文件的内容,然后将转换为JSON,并在HTTP响应中返回JSON

我的流配置如下所示

<flow name="test-flow">
        <http:listener config-ref="HttpListenerConfiguration" path="/read" allowedMethods="POST" doc:name="HTTP">
            <http:response-builder statusCode="200">
                <http:header headerName="Content-Type" value="application/json"/>
            </http:response-builder>
        </http:listener>
        <file:inbound-endpoint path="xml/test.xml" responseTimeout="10000" doc:name="File"/>
        <mulexml:xml-to-object-transformer returnClass="org.mule.examples.Catalog" doc:name="XML to Object"/>
        <json:object-to-json-transformer sourceClass="org.mule.examples.Catalog" doc:name="Object to JSON"/>
    </flow>

当然不行了。我得到一份工作

SAXParseException:cvc复杂类型。2.4.a:发现以元素“file:inbound endpoint”开头的无效内容。“{”之一http://www.mulesoft.org/schema/mule/core:抽象消息处理器http://www.mulesoft.org/schema/mule/core:抽象出站终结点http://www.mulesoft.org/schema/mule/core:抽象混合内容消息处理器http://www.mulesoft.org/schema/mule/core“:response}”应为空。

我认为这是因为
需要作为流中的源

关于调用HTTP端点后如何读取文件的任何建议


谢谢

试试这个。它将允许您在流中的任何点加载文件。你可以在这篇文章中了解更多

正如您正确假设的,file:inbound端点只能充当消息源。因此,为了实现您想要的,您可以使用。HTH.

如果XML文件在类路径上,您可以尝试
而不是


解析模板处理器经常出人意料地派上用场。《用户指南》也有一个很好的例子。

您还可以编写一个java组件来读取文件的内容,并在流中使用该组件

例如:

公共类FileReaderComponent实现可调用、可初始化{

private String filePath;

public String getFilePath() {
    return filePath;
}

public void setFilePath(String filePath) {
    this.filePath = filePath;
}

@Override
public void initialise() throws InitialisationException {
    if (filePath == null || filePath.equals("")) {
        throw new InitialisationException(MessageFactory.createStaticMessage("FilePath property has to be set"), this);
    }
}
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {

    BufferedReader in = null;
    try {
        in = new BufferedReader(new FileReader(new File(filePath)));
    } catch (Exception e) {
        //Handle Exception
    }

    StringBuilder content = new StringBuilder();

    try {

        String line;
        while ((line = in.readLine()) != null) {
          content.append(line);

        } 
    }catch (Exception e) {
        //Handle Exception
    } finally {
        in.close();
    }


    return content.toString();
}

}

正如上面所建议的,您可以使用java组件或某种解析模板


我们也有同样的需求,我们使用expression组件从特定位置读取文件

,您还可以使用XML-to-Json内置转换器将其转换为Json对象

<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="test-flow1">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
    <json:xml-to-json-transformer doc:name="XML to JSON"/>
    <logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
</flow>

,但请求者模块是首选方法。