【问题标题】:How to parse json in mule flow?如何在 mule 流中解析 json?
【发布时间】:2017-09-26 11:13:42
【问题描述】:

在我的 mule 流程中 - 尝试使用 post http 服务将值插入数据库。我可以成功地将帖子正文从输入流转换为 json。但是,当尝试在表中插入值时,只会插入空值。

流程:

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

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:json="http://www.mulesoft.org/schema/mule/json" 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/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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="9191" doc:name="HTTP Listener Configuration"/>
    <db:mysql-config name="MySQL_Configuration" host="#######" port="####" user="####" password="#####" database="#####" doc:name="MySQL Configuration"/>
    <flow name="patient-symptomFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/symptom" allowedMethods="POST" doc:name="HTTP"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <set-variable variableName="payload" value="#[payload]" doc:name="Variable"/>
        <byte-array-to-object-transformer doc:name="Byte Array to Object"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
        <db:insert config-ref="MySQL_Configuration" doc:name="Database">
            <db:parameterized-query><![CDATA[insert into test(uuid) values(#[payload.test])]]></db:parameterized-query>
        </db:insert>
    </flow>
</mule>

Json 输入:

{
    "test" : "success"
}

【问题讨论】:

  • 你能分享你的输入json吗?
  • @AbhaySingh - {“测试”:“成功”}

标签: mule mule-esb


【解决方案1】:

试试这个,根据你提供的输入,我能够得到“成功”的值。

    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
        <flow name="poc_Flow">
            <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
            <set-variable variableName="payload" value="#[payload]" mimeType="application/json" doc:name="Variable"/>
            <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
            <logger message="#[payload.test]" level="INFO" doc:name="Logger"/>
</flow>

【讨论】:

    【解决方案2】:
      <flow name="patient-symptomFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/symptom" allowedMethods="POST" doc:name="HTTP"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <set-variable variableName="payload" value="#[payload]" doc:name="Variable"/>
        <byte-array-to-object-transformer doc:name="Byte Array to Object"/>
        <db:insert config-ref="MySQL_Configuration" doc:name="Database">
            <db:parameterized-query><![CDATA[insert into test(uuid) values(#[json:test])]]></db:parameterized-query>
        </db:insert>
    </flow>
    

    这应该可行。无需转换为对象。

    【讨论】:

      【解决方案3】:

      试试这个:

      <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
      <flow name="poc_Flow">
                  <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
                  <set-variable variableName="payload" value="#[payload]" mimeType="application/json" doc:name="Variable"/>
                  <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
                  <logger message="#[payload.test]" level="INFO" doc:name="Logger"/>
      </flow>   
      

      【讨论】:

        猜你喜欢
        • 2012-12-02
        • 2016-12-30
        • 2011-09-24
        • 1970-01-01
        • 2012-06-11
        • 2015-05-16
        • 2014-10-27
        • 1970-01-01
        • 2014-03-01
        相关资源
        最近更新 更多