如何在MULE ESB中响应HTTP请求的自定义JSON [英] How to respond custom JSON to HTTP request in MULE ESB

查看:135
本文介绍了如何在MULE ESB中响应HTTP请求的自定义JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mule Studio,我正在连接到postgressql进行数据选择,我做得很好,我刚刚碰到中间.我正在向curl发出curl请求,像这样

i am using mule studio i am connecting to postgressql for data selection i am doing well i just struck in middle .i am sending curl request to mule like this

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

我从数据库获得响应,但无法按照客户端要求格式化数据. 我的配置是这样的

i am getting response from database but unable to format the data as per client requirement. my config is like this

<?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>
        <response>
            <http:response-builder status="200" contentType="application/json" doc:name="HTTP Response Builder"/>
        </response>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <response>
            <set-payload value="#[payload]" doc:name="Set Payload"/>
        </response>

                <echo-component doc:name="Echo"/>
            </flow>
</mule>

按照上述配置,我正在从数据库获取响应,但无法格式化 我的日志是这样的

as per above config i am getting response from database but unable to format my log is like this

org.mule.api.processor.LoggerMessageProcessor: [{lastname=sk, firstname=naryanan, id=1}]

但无法发送curl客户端,我希望以此格式将响应发送给客户端

but unable to send a curl client i wish to send response in this format to client

{"ResponseJSON": {"Body":{"Datalist": [{lastname=sk, firstname=naryanan, id=1}]},"Status":"200"}}

我如何在muleesb中获得这种格式

How i get this format in muleesb

推荐答案

使用expression-transformer生成Map of Map,将有效载荷包装在{"ResponseJSON": {"Body":{"Datalist"中.将此地图序列化为JSON,就可以完成.

Use an expression-transformer to build the a Map of Map of Map to wrap your payload in {"ResponseJSON": {"Body":{"Datalist". Serialize this map to JSON and you'll be done.

这篇关于如何在MULE ESB中响应HTTP请求的自定义JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆