如何将JDBC响应转换为HTTP响应在MULE ESB中

时间:2013-11-29 07:54:23

标签: mule mule-studio mule-el

我正在尝试使用mule studio从数据库中选择数据我已经完成了所有jdbc连接并从数据库获取数据。但是无法使用JSON响应客户端 我的客户端是CURL客户端将数据发送到JSON,就像这样

 curl -H "Content-Type: application/json" -d '{"id":"1"}' http://localhost:8081/selectdb 

并使用以下配置创建流程我在esb日志中收到数据库的响应但无法通过客户端我的配置就像这样

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

<mule xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:jdbc-ee="http://www.mulesoft.org/schema/mule/ee/jdbc" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" 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.4.1" 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/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.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/jdbc http://www.mulesoft.org/schema/mule/ee/jdbc/current/mule-jdbc-ee.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/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd">
    <jdbc-ee:postgresql-data-source name="PostgreSQL_Data_Source" user="youtilitydba" password="Youtility11" url="jdbc:postgresql://localhost:5432/sample" transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source"/>
    <jdbc-ee:connector name="Database" dataSource-ref="PostgreSQL_Data_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database"/>
    <flow name="selectfromdbFlow1" doc:name="selectfromdbFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="selectdb" doc:name="HTTP"/>
        <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>

        <jdbc-ee:outbound-endpoint exchange-pattern="request-response" queryKey="SELECT" queryTimeout="-1" connector-ref="Database" doc:name="Database">
            <jdbc-ee:query key="SELECT" value="select  firstname,lastname,id  from users where id =#[message.payload.id]"/>
        </jdbc-ee:outbound-endpoint>
        <logger message="message before set httpresponse #[payload]" level="INFO" doc:name="Logger"/>
        <response>
            <logger message="after expression #[payload]" level="INFO" doc:name="Logger"/>
        </response>
        <response>
            <expression-transformer expression="#[payload]" doc:name="Expression"/>
        </response>
            </flow></mule>

并且我记录了两次,但是同样的消息记录却无法向我的客户端发送像这样的日志

INFO  2013-11-29 12:57:12,589 [[selectfromdb].connector.http.mule.default.receiver.02] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'Database.dispatcher.1260873373'. Object is: EEJdbcMessageDispatcher
INFO  2013-11-29 12:57:12,589 [[selectfromdb].connector.http.mule.default.receiver.02] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'Database.dispatcher.1260873373'. Object is: EEJdbcMessageDispatcher
INFO  2013-11-29 12:57:12,637 [[selectfromdb].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: message before set httpresponse [{lastname=ff, firstname=ff, id=5}]
INFO  2013-11-29 12:57:12,639 [[selectfromdb].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: after expression [{lastname=ff, firstname=ff, id=5}]

但不是来自客户端 尝试了 setpayload,echo,httpresponsebuilder,expression-transfomer ,但是我无法得到这个

2 个答案:

答案 0 :(得分:0)

在查询之后,您必须使用Object to JSON Transformer,然后添加具有以下属性的HTTP Response Builder:contentType=application/jsonstatus=200

以下是一个例子:

<flow name="DB-Read-TestFlow1" doc:name="DB-Read-TestFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="selectdb" doc:name="HTTP"/>
    <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>

    <jdbc-ee:outbound-endpoint exchange-pattern="request-response" queryKey="SELECT" queryTimeout="-1" connector-ref="Database" doc:name="Database">
        <jdbc-ee:query key="SELECT" value="select  firstname,lastname,id  from users where id =#[message.payload.id]"/>
    </jdbc-ee:outbound-endpoint>
    <logger message="message before set httpresponse #[payload]" level="INFO" doc:name="Logger"/>
    <response>
        <logger message="after expression #[payload]" level="INFO" doc:name="Logger"/>
    </response>
    <json:object-to-json-transformer doc:name="Object to JSON"/>
    <response>
        <http:response-builder status="200" contentType="application/json" doc:name="HTTP Response Builder"/>
    </response>
</flow>

答案 1 :(得分:0)

您可以在结尾处直接使用<http:response-builder/>和您的状态代码,并将消息设置为有效负载
类似于以下内容: -

<http:response-builder status="200" contentType="application/json" doc:name="HTTP Response Builder">
<set-payload value="Your Message" />
</http:response-builder>