json - Mule ESB,以类似 JSON 的格式返回数据库连接器数据

标签 json rest http mule esb

我有一个简单的流程,它接收一个 REST,但没有任何相应的数据被输入到流程中

XML如下:

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

<mule xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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.5.0"
    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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.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/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <db:generic-config name="Generic_Database_Configuration" url="jdbc:db2://localhost:50000/TEST:user=instuid;password=instpw;" driverClassName="com.ibm.db2.jcc.DB2Driver" doc:name="Generic Database Configuration"/>
    <flow name="test2Flow1" doc:name="test2Flow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <db:select config-ref="Generic_Database_Configuration" doc:name="Database" doc:description="test">
            <db:parameterized-query><![CDATA[SELECT ACCTNUM FROM xxx.ACCOUNT]]></db:parameterized-query>
        </db:select>
    </flow>
</mule>

来自命令行(Mule 之外)的 SQL 返回以下内容:

db2 => SELECT ACCTNUM FROM xxx.ACCOUNT

ACCTNUM
----------
11111
22222

流程然后执行一个完全配置的数据库连接器,数据 10 被传递到出站 http 连接器:

C:\curl>curl http://localhost:8081
¼φ ♣sr ¶java.util.LinkedList♀)S]J`ê"♥  xpw♦   ☻sr $org.mule.util.CaseInsensitive
HashMap¥╤┘∩gE╬ ♥  xpw♀?@     ►   ☺t ACCTNUMt
11111     xsq ~ ☻w♀?@     ►   ☺q ~ ♦t
22222     xx

...因此您可以看到它正在从表“11111”和“22222”中获取变量。

我现在还不知道该怎么办。我想获取数据库连接器的结果,并返回字符串“11111”和“22222”。以类似 JSON 的格式,但我不知道该怎么做

有人可以告诉我最简单的方法吗,也许可以提供一个例子?

谢谢

最佳答案

是的,你做对了....只需在末尾的数据库连接器之后放置一个对象到 JSON 转换器,如 <json:object-to-json-transformer doc:name="Object to JSON"/> ...请参阅以下内容:-

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

<mule xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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.5.0"
    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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.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/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <db:generic-config name="Generic_Database_Configuration" url="jdbc:db2://localhost:50000/TEST:user=instuid;password=instpw;" driverClassName="com.ibm.db2.jcc.DB2Driver" doc:name="Generic Database Configuration"/>
    <flow name="test2Flow1" doc:name="test2Flow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <db:select config-ref="Generic_Database_Configuration" doc:name="Database" doc:description="test">
            <db:parameterized-query><![CDATA[SELECT ACCTNUM FROM xxx.ACCOUNT]]></db:parameterized-query>
        </db:select>
  <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>
</mule>

关于json - Mule ESB,以类似 JSON 的格式返回数据库连接器数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24728604/

相关文章:

mysql - 如何在事件日志的一个 "activity"中存储(在 MySQL 中)多个更新?

java - 在 Spring MVC 3 中指定 HTTP "Location"响应 header 的首选方法是什么?

android - 如何使用 Retrofit 将包含图像的类从 Android 发送到 Java Restful Web 服务器

wcf - Azure 并发提升?

http - 如何检测 Content-Length 不同主体大小的 HTTP 响应主体的结尾?

javascript - 如何在javaScript中添加变量作为json键

javascript - emacs - 从 js-mode-hook 中排除 json-mode

java - 解析json的问题

rest - Powershell v3 调用 RestMethod

http - 什么叫请求-响应对?