Mule: JSON to Object
-
Upload
sulthony-hartanto -
Category
Software
-
view
406 -
download
1
Transcript of Mule: JSON to Object
![Page 1: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/1.jpg)
MULE: JSON TO OBJECT
HANDLING UNRECOGNIZED FIELD
![Page 2: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/2.jpg)
JSON SUPPORT REFERENCE• JSON, short for JavaScript Object Notation, is a lightweight data
interchange format. • Mule supports binding JSON data to objects and marshalling Java
object to JSON using the Jackson Framework.• JSON transformers have been added in Studio to make it easy to
work with JSON encoded messages.
![Page 3: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/3.jpg)
JSON BINDINGS (1/2)• Jackson uses annotations to describe how data is mapped to a Java
object model.• For example an JSON file that describes a person:
{ "id": 1, "name": "Max"}
![Page 4: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/4.jpg)
JSON BINDINGS (2/2)• @JsonAutoDetect maps directly the field member names to JSON
field names.package com.mulesoft.learning;
import org.codehaus.jackson.annotate.JsonAutoDetect;
@JsonAutoDetectpublic class Person {
private String id; private String name; //getter-setter}
![Page 5: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/5.jpg)
JSON TO OBJECT: DEMO - DESIGN• HTTP: an inbound endpoint for posting the JSON data.• JSON to Object: transforming the JSON data to a Java object.• Logger: displaying the log.
![Page 6: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/6.jpg)
JSON TO OBJECT: DEMO - XML CONFIG• Two-way editing between graphical and XML views.
<flow name="jsonToObjectFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> <json:json-to-object-transformer returnClass="com.mulesoft.learning.Person" doc:name="JSON to Object"/> <logger message="#[payload]" level="INFO" doc:name="Logger"/></flow>
![Page 7: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/7.jpg)
JSON TO OBJECT: TESTING (1/2)• Use a REST Client, e.g.: Postman, for posting the JSON data.
![Page 8: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/8.jpg)
JSON TO OBJECT: TESTING (2/2)• Posting the JSON data with an unrecognized field: Failed!
![Page 9: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/9.jpg)
JSON TO OBJECT: ISSUE• JSON to Object Transformer failed to transform an JSON data with
unrecognized field.• Root Exception stack trace:
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "type" (Class com.mulesoft.learning.Person), not marked as ignorable.
• How to easily adapt to change if this issue happens?
![Page 10: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/10.jpg)
JSON TO OBJECT: SOLUTION• Ignore unrecognized field.
package com.mulesoft.learning;
import org.codehaus.jackson.annotate.JsonAutoDetect;import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonAutoDetect@JsonIgnoreProperties(ignoreUnknown=true)public class Person {
private String id; private String name; //getter-setter}
![Page 11: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/11.jpg)
JSON TO OBJECT: FINAL TESTING• Posting the JSON data with an unrecognized field: Success!
![Page 12: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/12.jpg)
SUMMARY• JSON is natively supported in Mule.• JSON data can be bind automatically to Java object.• Unrecognized field can be handled by Jackson Framework.
![Page 13: Mule: JSON to Object](https://reader036.fdocuments.net/reader036/viewer/2022062412/586fdca31a28ab18428b657d/html5/thumbnails/13.jpg)
RESOURCES• https://
docs.mulesoft.com/mule-user-guide/v/3.8/json-module-reference• https://
docs.mulesoft.com/mule-user-guide/v/3.8/native-support-for-json