简体   繁体   中英

Insert json object in mysql DB with Mule ESB

Good evening!

I'm trying to insert an entire json object into mysql table. I'm using json to object transformer to convert json into HashMap. Json is this:

    {
   "content": {
      "fill": "none",
      "stroke": "#fff",
      "path": [
        ["M", 422, 115],
        ["L", 472, 167.5]
      ],
      "stroke-width": 4,
      "stroke-linecap": "round",
      "stroke-linejoin": "round",
      "transform": [],
      "type": "path",
      "note": {
        "id": 47,
        "page":0, 
        "ref": 3,
        "txt": "teste do serviço",
        "addedAt": 1418133743604,
        "addedBy": "valter.gomes"
      }
  }
}

I need insert "content" object, but when I try access it by #[payload.content] , threws an exception :

Root Exception stack trace:
java.sql.SQLException: Incorrect string value: '\xAC\xED\x00\x05sr...' for column 'content' at row 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)

We found what I think is a workaround. Before convert into a HashMap I get "content" object into a variable #[json:content] and record it in DB #[flowVars.rawContent] . When I retrive it from DB I convert ResultSet into String using Object to String converter.

But, Im not confortable with this solution. Is this the right way to do so? Or does exist other one ? Maybe the right one.

Tks a lot for your help.

When you receive the json you can transform to Map class (By default json:json-to-object-transformer return JsonData). For that reason I have specified Map class. So, after that you can read content from payload using #[payload.content]

I attached my flow:

<flow name="demoFlow1" doc:name="demoFlow1">     
    <http:inbound-endpoint exchange-pattern="request-response"
            host="localhost" port="8081" path="demo" doc:name="HTTP" />
    <scripting:component doc:name="Groovy">
       <scripting:script engine="Groovy"><![CDATA[
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("fill","none");
            map1.put("stroke","#fff");  
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("content", map1);
            return map;]]></scripting:script>
    </scripting:component>
    <json:object-to-json-transformer doc:name="Object to JSON"/>
    <logger level="INFO" message=">>1 #[payload]" doc:name="Logger" />
    <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>     
    <set-payload value="#[payload.content]" doc:name="Set Payload"/>
    <json:object-to-json-transformer doc:name="Object to JSON"/>
    <logger level="INFO" message=">>3 #[payload]" doc:name="Logger" />
</flow>

Eddú is right, but the example he gives is really too complex.

As he said, all you need is:

<json:json-to-object-transformer returnClass="java.util.Map" />

After that transformer, you can retrieve any field/sub-field in the Map. I suggest using message.payload instead of payload by the way, the latter has shown some odd behaviours in the past.

So use: #[message.payload.content]

Also, this will give you an object of type java.util.Map . Not sure how you're going to insert the object in the DB but since you are not showing this part in your question, I imagine you'll figure it out...

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