如何使用Mule ESB,Mule Studio读取返回JSON对象并将其存储到PostgreSQL的REST服务 [英] How to read REST service that returns JSON object and store the same into PostgreSQL using Mule ESB, Mule Studio

查看:67
本文介绍了如何使用Mule ESB,Mule Studio读取返回JSON对象并将其存储到PostgreSQL的REST服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个基于REST的服务,托管在

So I have a REST based service which is hosted at

http://localhost:35798/RestServiceImpl.svc/json/567

如果我对此进行查询,则得到的结果为:

If I query that, I get the result as:

{"JSONDataResult":"You requested product 567"}

我需要将整个JSON数据存储到PostgreSQL表中

I need to store the whole JSON data into a PostgreSQL table:

CREATE TABLE "JsonTable"
(
  "StoreJsonObject" json
)

如果我想解析值字段,即您请求的产品567",则该程序有效(此处我使用的列类型为text的其他表):

If I want to parse the value field i.e. "You requested product 567", the program works (here I am using a different table whose column type is text):

<jdbc:postgresql-data-source name="PostgreSQL_Data_Source" user="username" password="pwd" url="jdbc:postgresql://localhost:5432/TestDB" transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source"/>
<jdbc:connector name="PostgreSQL_Connector" dataSource-ref="PostgreSQL_Data_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database">
    <jdbc:query key="InsertRecord" value="INSERT INTO &quot;AnotherJSonTable&quot;(&quot;StoreJsonObject&quot;) VALUES (#[message.payload])"/>
</jdbc:connector>
<flow name="testRestFlow1" doc:name="testRestFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" address="http://localhost:8082/index.html"  doc:name="HTTP"/>
    <http:rest-service-component httpMethod="GET" serviceUrl="http://localhost:35798/RestServiceImpl.svc/json/567">
    </http:rest-service-component>
    <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
    <expression-transformer expression="#[message.payload.JSONDataResult]" doc:name="Expression"/>
    <jdbc:outbound-endpoint exchange-pattern="one-way" queryKey="InsertRecord" queryTimeout="-1" connector-ref="PostgreSQL_Connector" doc:name="Database"/>
</flow>

但是如何存储整个JSON对象({"JSONDataResult":您请求的产品567"}).
我需要在表达式变压器"中进行哪些更改?

But how to store the entire JSON object ({"JSONDataResult":"You requested product 567"}).
What do I need to change in the "expression-transformer"?

如果我这样做:

<jdbc:query key="InsertRecord" value="INSERT INTO &quot;JsonTable&quot;(&quot;StoreJsonObject&quot;) VALUES (#[message.payload])"/>

<expression-transformer expression="#[message.payload]" doc:name="Expression"/>

我收到异常:

Root Exception stack trace:
org.postgresql.util.PSQLException: No hstore extension installed.
    at org.postgresql.jdbc2.AbstractJdbc2Statement.setMap(AbstractJdbc2Statement.java:1713)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1916)
    at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:36)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

INFO  2012-12-24 15:48:31,945 [[testrest].connector.file.mule.default.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: C:\Users\niladri.biswas\Desktop\input\all_winjs_ui_controls.txt
INFO  2012-12-24 15:48:31,945 [[testrest].testRestFlow1.stage1.02] org.mule.transport.http.components.RestServiceWrapper: Invoking REST service: http://localhost:35798/RestServiceImpl.svc/json/567
ERROR 2012-12-24 15:48:31,992 [[testrest].PostgreSQL_Connector.dispatcher.01] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Failed to route event via endpoint: DefaultOutboundEndpoint{endpointUri=jdbc://InsertRecord, connector=JdbcConnector
{
  name=PostgreSQL_Connector
  lifecycle=start
  this=15e7ea6
  numberOfConcurrentTransactedReceivers=4
  createMultipleTransactedReceivers=false
  connected=true
  supportedProtocols=[jdbc]
  serviceOverrides=<none>
}
,  name='endpoint.jdbc.InsertRecord', mep=ONE_WAY, properties={queryTimeout=-1}, transactionConfig=Transaction{factory=null, action=INDIFFERENT, timeout=0}, deleteUnacceptedMessages=false, initialState=started, responseTimeout=10000, endpointEncoding=UTF-8, disableTransportTransformer=false}. Message payload is of type: LinkedHashMap
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------

此外,从服务读取后,记录只能插入一次,而不能多次插入.

Also the record should be inserted only once after reading from the service and not multiple times.

推荐答案

由于要存储整个JSON,因此无需将其反序列化为一个对象:我建议您将HTTP流传输的有效负载简单地转换为java.lang.String并将其原样插入数据库.

Since you want to store the whole JSON, there is no need to deserialize it as an object: I suggest you simply transform the HTTP-streamed payload into a java.lang.String and insert it as-is in the DB.

这将是这样的:

<jdbc:postgresql-data-source name="PostgreSQL_Data_Source"
    user="username" password="pwd" url="jdbc:postgresql://localhost:5432/TestDB"
    transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source" />

<jdbc:connector name="PostgreSQL_Connector" dataSource-ref="PostgreSQL_Data_Source"
    validateConnections="true" queryTimeout="-1" pollingFrequency="0"
    doc:name="Database">
    <jdbc:query key="InsertRecord"
        value="INSERT INTO &quot;AnotherJSonTable&quot;(&quot;StoreJsonObject&quot;) VALUES (CAST(#[message.payload] AS json))" />
</jdbc:connector>

<flow name="testRestFlow1" doc:name="testRestFlow1">
    <http:inbound-endpoint exchange-pattern="request-response"
        address="http://localhost:8082/index.html" doc:name="HTTP" />
    <http:rest-service-component httpMethod="GET"
        serviceUrl="http://localhost:35798/RestServiceImpl.svc/json/567" />
    <object-to-string-transformer />
    <jdbc:outbound-endpoint exchange-pattern="one-way"
        queryKey="InsertRecord" queryTimeout="-1" connector-ref="PostgreSQL_Connector"
        doc:name="Database" />
</flow>

这篇关于如何使用Mule ESB,Mule Studio读取返回JSON对象并将其存储到PostgreSQL的REST服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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