简体   繁体   中英

Mule and JDBC Query Result

I am using mule to connect to a Postgres database server. I can't use the datamapper since I am using the community edition. The object to xml transformer is not giving a good formatted result. Any ideas of how I can transform the jdbc response into a good format for parsing. Here is my configuration file. This is the datasource with the connector.

<jdbc:postgresql-data-source name="PostgreSQL_Data_Source"
    user="USERNAME" password="PASSWORD"
    url="jdbc:postgresql://*******:***/*****"
    transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source" />
    <jdbc:connector name="organizations" dataSource-ref="PostgreSQL_Data_Source"
        validateConnections="true" queryTimeout="-1" pollingFrequency="0"
        doc:name="JDBC">
        <jdbc:query key="getOrganizations" value="SELECT * FROM organization" />
        <jdbc:query key="getOrganizationApplications"
            value="SELECT * FROM organization o,application a,organization_apps oa WHERE o.id=oa.org_id AND a.id=oa.app_id AND o.id=#[flowVars['org_id']]" />
        <jdbc:query key="insertOrganization"
            value="INSERT INTO organization(name, phone, email, address, website) VALUES (?, ?, ?, ?, ?)" />
        <jdbc:query key="getOrganizationUsers"
            value="SELECT * FROM &amp;quot;public&amp;quot;.user WHERE org_id=#[flowVars['org_id']]" />
    </jdbc:connector>

and this is the flow:

<flow name="logixy-platform-organizations-workflow" doc:name="logixy-platform-organizations-workflow">
        <http:inbound-endpoint exchange-pattern="request-response"
            host="localhost" port="8082" doc:name="HTTP" path="organization" />
        <set-variable variableName="choice"
            value="#[message.inboundProperties['choice']]" doc:name="Set Choice" />
        <choice doc:name="Choice">
            <when expression="#[flowVars['choice'] == '0']">
                <jdbc:outbound-endpoint exchange-pattern="request-response"
                    queryTimeout="-1" doc:name="getAllOrganizations" connector-ref="organizations"
                    queryKey="getOrganizations" />
            </when>
            <when expression="#[flowVars['choice'] == '2']">
                <set-variable variableName="org_id"
                    value="#[(int)(message.inboundProperties['org_id'])]" doc:name="Set Organization ID" />
                <jdbc:outbound-endpoint exchange-pattern="request-response"
                    queryKey="getOrganizationUsers" queryTimeout="-1" connector-ref="organizations"
                    doc:name="getOrganizationUsers" />
            </when>
            <when expression="#[flowVars['choice'] == '1']">
                <set-variable variableName="#['org_id']"
                    value="#[(int)(message.inboundProperties['org_id'])]" doc:name="Set Organization ID" />
                <jdbc:outbound-endpoint exchange-pattern="request-response"
                    queryKey="getOrganizationApplications" queryTimeout="-1"
                    connector-ref="organizations" doc:name="getOrganizationApplications" />
            </when>
        </choice>
        <mulexml:object-to-xml-transformer
            doc:name="Object to XML" />
    </flow>

You should really define "a good format for parsing", but I guess you don't like the "entry", "string", etc field names.

You have plenty of options to format the XML, for example:

  1. use XSLT to transform the generated XML to your preferred format
  2. define a class in Java or Groovy that corresponds to your return data and then either
    1. use object-to-json and then json-to-object with your class as return class
    2. assign the return data to objects of your class in Java or Groovy

Once you have your data as custom objects, object-to-xml-transformer will print produce a much cleaner output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM