Mule - 如何解析REST webservice的JSON响应和更新数据库 [英] Mule - how to parse the JSON response of REST webservice and update database
问题描述
我想编写一个mule应用程序,它将读取未处理记录的数据库,将它们分配到JSON有效负载中,然后点击REST webservice,REST webservice(托管在不同的服务器上)将处理记录并返回JSON输出。现在我必须解析JSON并更新数据库的已处理标志。
我能够从REST webservice获取响应,我已经使用Byte数组来进行字符串转换以登录记录器。
我需要编写foreach组件来逐个处理有效负载并更新数据库吗?
这里是我的配置XML
<?xml version =1.0encoding =UTF-8?&
< mule xmlns:oauth2 =http://www.mulesoft.org/schema/mule/oauth2xmlns:db =http://www.mulesoft.org/schema/mule / dbxmlns:json =http://www.mulesoft.org/schema/mule/jsonxmlns:http =http://www.mulesoft.org/schema/mule/httpxmlns =http: //www.mulesoft.org/schema/mule/corexmlns:doc =http://www.mulesoft.org/schema/mule/documentation
xmlns:spring =http:// www。 springframework.org/schema/beans
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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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/oauth2 http://www.mulesoft.org/schema/mule/oauth2/current/mule-oauth2.xsd\">
< db:mysql-config name =MySQL_Configurationhost =localhostport =3306user =rootdatabase =my_db_namedoc:name =MySQL Configuration/&
< http:request-config name =HTTP_Request_Configurationprotocol =HTTPShost =example.netport =8000basePath =Salvagedoc:name =HTTP Request Configuration/>
< flow name =cwg_clientFlow>
< poll doc:name =Poll>
< db:select config-ref =MySQL_Configurationdoc:name =Database>
< db:parameterized-query><![CDATA [SELECT * FROM cwg_ws_data WHERE SyncFlag = 0]]< / db:parameterized-query&
< / db:select>
< / poll>
< logger message =#[payload]level =INFOdoc:name =Logger/>
< json:object-to-json-transformer doc:name =Object to JSONencoding =UTF-8mimeType =application / json/>
< logger message =JSON Payload is#[payload]level =INFOdoc:name =Logger/>
< http:request config-ref =HTTP_Request_Configurationpath =/ muleCWGmethod =POSTdoc:name =HTTP>
< http:request-builder>
< http:header headerName =access_tokenvalue =U9P4CjhCsadIQzfJi13dHYdQLCfhmAi9OxYM8d7c/>
< / http:request-builder>
< http:success-status-code-validator values =200/>
< / http:request>
< byte-array-to-string-transformer doc:name =Byte Array to String/>
< logger message =webservice response#[payload]level =INFOdoc:name =Logger/>
< / flow>
< / mule>
请咨询。
< kendreparesh@gmail.com)
它不需要什么是记录计数批量更新可以在数千条记录上工作。您可以尝试以下类似的操作
: < flow name =testdbFlow>
< http:listener config-ref =HTTP_Listener_Configurationpath =/ testdoc:name =HTTP/>
< set-payload value ={& quot; 1234567890123456& quot;:& quot; Y& quot;,& quot; 1234567890123457& quot;:& mimeType =application / jsondoc:name =Set Payload/>
< dw:transform-message doc:name =Transform Message>
< dw:input-payload />
< dw:set-payload><![CDATA [%dw 1.0
%output application / json
---
(payload mapObject {
x: {
key:$$,
value:$
}
})。* x]]>
< / dw:set-payload>
< / dw:transform-message>
< json:json-to-object-transformer returnClass =java.util.Listdoc:name =JSON to Object/>
< db:update config-ref =MySQL_ConfigurationbulkMode =truedoc:name =Database>
< db:dynamic-query><![CDATA [UPDATE cwg_ws_data SET SyncFlag ='#[payload.value]'WHERE IMEI ='#[payload.key]']]>< / db :dynamic-query>
< / db:update>
< / flow>
我已经测试了这个输入和它的工作正常。 FYI dataweave用于将json转换为有意义且可访问的有效内容。
希望这有助于。
I want to write a mule application which will read the database for unprocessed records, club them in JSON payload and then hit a REST webservice, REST webservice(Hosted on different server) will process the records and return the JSON output. Now I have to parse the JSON and update the database for the processed flag.
I am able to get the response from the REST webservice, I have used Byte array to string transform to log in the logger.
Do I need to write foreach component to process the payload one by one and update the database?
here is my configuration XML
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:oauth2="http://www.mulesoft.org/schema/mule/oauth2" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:json="http://www.mulesoft.org/schema/mule/json" 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"
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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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/oauth2 http://www.mulesoft.org/schema/mule/oauth2/current/mule-oauth2.xsd">
<db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="root" database="my_db_name" doc:name="MySQL Configuration"/>
<http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="example.net" port="8000" basePath="Salvage" doc:name="HTTP Request Configuration"/>
<flow name="cwg_clientFlow">
<poll doc:name="Poll">
<db:select config-ref="MySQL_Configuration" doc:name="Database">
<db:parameterized-query><![CDATA[SELECT * FROM cwg_ws_data WHERE SyncFlag = 0]]></db:parameterized-query>
</db:select>
</poll>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
<json:object-to-json-transformer doc:name="Object to JSON" encoding="UTF-8" mimeType="application/json"/>
<logger message="JSON Payload is #[payload]" level="INFO" doc:name="Logger"/>
<http:request config-ref="HTTP_Request_Configuration" path="/muleCWG" method="POST" doc:name="HTTP">
<http:request-builder>
<http:header headerName="access_token" value="U9P4CjhCsadIQzfJi13dHYdQLCfhmAi9OxYM8d7c"/>
</http:request-builder>
<http:success-status-code-validator values="200"/>
</http:request>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger message="webservice response #[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
Please advice.
-Paresh (kendreparesh@gmail.com)
It doesn,t matter what is the count of records Bulk update can works on thousands of records. You can try something like following
<flow name="testdbFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<set-payload value="{"1234567890123456":"Y","1234567890123457":"Y"}" mimeType="application/json" doc:name="Set Payload"/>
<dw:transform-message doc:name="Transform Message">
<dw:input-payload />
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
(payload mapObject {
x: {
key : $$,
value : $
}
}).*x]]>
</dw:set-payload>
</dw:transform-message>
<json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"/>
<db:update config-ref="MySQL_Configuration" bulkMode="true" doc:name="Database">
<db:dynamic-query><![CDATA[UPDATE cwg_ws_data SET SyncFlag = '#[payload.value]' WHERE IMEI = '#[payload.key]']]></db:dynamic-query>
</db:update>
</flow>
I have tested this with same input and its working fine. FYI dataweave is used for converting json to meaningful and accessible payload.
Hope this helps.
这篇关于Mule - 如何解析REST webservice的JSON响应和更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!