首页 > 解决方案 > 如何在骡流中显示来自json的特定字段

问题描述

嗨,我正在尝试创建一个简单的 mule 流,在其中我从 rest url 获取 json 数据,
例如。{“令牌”:123,“id”:456,“电子邮件”:“abc@abc.com”,“状态”:“成功”}

现在我希望我的回复只显示 2 个字段,所以我的最终输出 json 将是这样的..

例如。{“id”:456,“电子邮件”:“abc@abc.com”}

我知道这是非常基本的。如果有人能帮助我,我会很高兴,因为我对骡子很基础。谢谢!

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

                <mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" 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" 
                    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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
                http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
                    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
                    <http:request-config name="HTTP_Request_Configuration" host="reqres.in" port="8080" doc:name="HTTP Request Configuration"/>
                    <flow name="testFlow">
                        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" allowedMethods="GET" doc:name="HTTP"/>
                        <http:request config-ref="HTTP_Request_Configuration" path="/api/users/2" method="GET" doc:name="HTTP"/>

                        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
                    </flow>
                </mule>

标签: jsonresturlmulemule-component

解决方案


由于您是 mule 新手,您可以使用简单的 set payload 和一些 json 路径表达式来尝试这个简单的解决方案。

<flow name="jsonTransform">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/jsonTransform" doc:name="HTTP" allowedMethods="POST"/>
        <set-payload value="{ &quot;id&quot; : &quot;#[json:id]&quot;, &quot;email&quot; : &quot;#[json:email]&quot; }" doc:name="Set Payload"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>

推荐阅读