Java 如何在Mule中动态地将json数据插入数据库
嗨,我有一个json文件,如下所示 JSON:Java 如何在Mule中动态地将json数据插入数据库,java,json,mule,anypoint-studio,Java,Json,Mule,Anypoint Studio,嗨,我有一个json文件,如下所示 JSON: [ { "name": "Steve", "salary": "$1000", "age": "26" }, { "name': "Laura", "salary': "$1500", "age": "28" }, { "name': "Jack", "salary": "$2000
[
{
"name": "Steve",
"salary": "$1000",
"age": "26"
},
{
"name': "Laura",
"salary': "$1500",
"age": "28"
},
{
"name': "Jack",
"salary": "$2000",
"age": "30"
}
]
我还有一个数据库表调用Employee,它有三列:name、age和salary。这里我想处理Json数据并将其存储到Employee表中。那我在骡子里怎么做呢?像这样的
类似的东西
使用以下方法创建您的mule流程:
forEach
使用以下方法创建您的骡子流:
forEach
首先,您的json请求无效,您需要在json键中放置
“
而不是”
。因此,有效的json将是:-
[
{
"name": "Steve",
"salary": "$1000",
"age": "26"
},
{
"name": "Laura",
"salary": "$1500",
"age": "28"
},
{
"name": "Jack",
"salary": "$2000",
"age": "30"
}
]
您需要传递来自浏览器客户端的请求:-
现在,您可以使用以下Mule流插入数据库:-
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"></http:listener-config>
<flow name="Flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/set" doc:name="HTTP"></http:listener>
<json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"></json:json-to-object-transformer>
<foreach collection="#[message.payload]" doc:name="For Each">
<db:insert config-ref="Generic_Database_Configuration" doc:name="Database">
<db:parameterized-query><![CDATA[INSERT into table1 (NAME, SALARY, AGE) VALUES (#[message.payload.name],#[message.payload.salary],#[message.payload.age]);]]></db:parameterized-query>
</db:insert>
</foreach>
<set-payload value="Inserted" doc:name="Set Payload"/>
</flow>
由于此处的字符串首先您的json请求无效,您需要将
“
而不是”
放在json键中。因此,有效的json将是:-
[
{
"name": "Steve",
"salary": "$1000",
"age": "26"
},
{
"name": "Laura",
"salary": "$1500",
"age": "28"
},
{
"name": "Jack",
"salary": "$2000",
"age": "30"
}
]
您需要传递来自浏览器客户端的请求:-
现在,您可以使用以下Mule流插入数据库:-
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"></http:listener-config>
<flow name="Flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/set" doc:name="HTTP"></http:listener>
<json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"></json:json-to-object-transformer>
<foreach collection="#[message.payload]" doc:name="For Each">
<db:insert config-ref="Generic_Database_Configuration" doc:name="Database">
<db:parameterized-query><![CDATA[INSERT into table1 (NAME, SALARY, AGE) VALUES (#[message.payload.name],#[message.payload.salary],#[message.payload.age]);]]></db:parameterized-query>
</db:insert>
</foreach>
<set-payload value="Inserted" doc:name="Set Payload"/>
</flow>
首先,正如Anirban所说,您在这里提供的JSON数据无效,请确保整数数据值(即此处的年龄)不要用双引号括起来 您还可以使用Dataweave transformer,因为大多数mule开发人员都喜欢将mule消息转换为对象转换器,而不是json。显然,json到对象转换器也可以正常工作,但大多数开发人员现在都在使用Dataweave 配置xml文件如下:-
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:db="http://www.mulesoft.org/schema/mule/db"
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.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" connectionIdleTimeout="3000000" doc:name="HTTP Listener Configuration"/>
<db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" database="provide your schema/database name here" doc:name="MySQL Configuration" password="provide username here" user="provide password here"/>
<flow name="muleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/postEmp" allowedMethods="POST" doc:name="HTTP"/>
<dw:transform-message metadata:id="35bfe913-8de7-4b3c-9ba9-98f375a2873e" doc:name="Transform Message">
<dw:input-payload mimeType="application/json"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload map ((payload01 , indexOfPayload01) -> {
id: payload01.id,
name: payload01.name,
designation: payload01.designation,
salary: payload01.Salary
})]]></dw:set-payload>
</dw:transform-message>
<foreach doc:name="For Each">
<db:insert config-ref="MySQL_Configuration" doc:name="Database-Insert">
<db:parameterized-query><![CDATA[insert into fulltime_employee values (#[payload.id], #[payload.name], #[payload.designation], #[payload.salary])]]></db:parameterized-query>
</db:insert>
</foreach>
</flow>
</mule>
{
id:payload01.id,
名称:payload01.name,
名称:payload01.designation,
薪水:薪水01.薪水
})]]>
当您使用Transform message transformer时,它将请求源和目标元数据。这里,源元数据将是json列表,目标元数据将是map列表。要设置源元数据,只需单击源部件的添加元数据蓝色链接,就会打开一个新的弹出窗口,从这里通过单击绿色加号添加一个新元数据,并给出一个名称,将其类型设置为JSON,从下面的下拉列表中设置示例,浏览JSON文件,它将自动填充元数据道具。对于目标元数据,请选择MAP作为类型,并通过单击下面框中的绿色符号添加名称、薪资和年龄道具。请记住,此属性名称应与您提到的db列名相同。首先,您在此处提供的JSON数据与Anirban所说的不符,请确保使用整型数据值(即此处的年龄)不要用双引号括起来 您还可以使用Dataweave transformer,因为大多数mule开发人员都喜欢将mule消息转换为对象转换器,而不是json。显然,json到对象转换器也可以正常工作,但大多数开发人员现在都在使用Dataweave 配置xml文件如下:-
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:db="http://www.mulesoft.org/schema/mule/db"
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.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" connectionIdleTimeout="3000000" doc:name="HTTP Listener Configuration"/>
<db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" database="provide your schema/database name here" doc:name="MySQL Configuration" password="provide username here" user="provide password here"/>
<flow name="muleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/postEmp" allowedMethods="POST" doc:name="HTTP"/>
<dw:transform-message metadata:id="35bfe913-8de7-4b3c-9ba9-98f375a2873e" doc:name="Transform Message">
<dw:input-payload mimeType="application/json"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload map ((payload01 , indexOfPayload01) -> {
id: payload01.id,
name: payload01.name,
designation: payload01.designation,
salary: payload01.Salary
})]]></dw:set-payload>
</dw:transform-message>
<foreach doc:name="For Each">
<db:insert config-ref="MySQL_Configuration" doc:name="Database-Insert">
<db:parameterized-query><![CDATA[insert into fulltime_employee values (#[payload.id], #[payload.name], #[payload.designation], #[payload.salary])]]></db:parameterized-query>
</db:insert>
</foreach>
</flow>
</mule>
{
id:payload01.id,
名称:payload01.name,
名称:payload01.designation,
薪水:薪水01.薪水
})]]>
当您使用Transform message transformer时,它将请求源和目标元数据。这里,源元数据将是json列表,目标元数据将是map列表。要设置源元数据,只需单击源部件的添加元数据蓝色链接,就会打开一个新的弹出窗口,从这里通过单击绿色加号添加一个新元数据,并给出一个名称,将其类型设置为JSON,从下面的下拉列表中设置示例,浏览JSON文件,它将自动填充元数据道具。对于目标元数据,选择MAP作为类型,并通过单击下面框中的绿色符号添加姓名、薪资和年龄道具。请记住,此属性名称应与您提到的db列名相同