REST response formats
REST services support JSON and XML formats for input and output data by default. The default format is JSON.
When an XML String is sent to a REST service, you must set Content-Type HTTP header to be "application/xml". The REST service converts the XML input data to the JSON format that is accepted by REST services. Therefore, your input XML format must follow a certain structure so that it can be transformed to the JSON object that is accepted by REST services. When requesting XML response format for a REST service, com.ibm.commerce.rest.utils.MapToXMLUtil.serializeMapToXML is called to transform a JSON object to an XML string. You must keep the same XML format when passing it back as input data for REST services. Otherwise, REST services might not be able to parse the string to get the correct input data.
XML format and JSON format mapping
The following snippets demonstrate examples for how JSON formats and XML formats are transformed to each other.
Example: A flat map, where one value is a list of type String:
- JSON
{ "firstName" : "John", "lastName" : "Smith", "addressLine": [ "100 main st.", "Suite 100", "" ], "city" : "RTP", "state" : "North Carolina", "country" : "United States", "zipCode" : "27560", "phone1" : "919-111-1111", "email1" : "u1@m.com", }
- XML
<?xml version="1.0" encoding="UTF-8" ?> <root> <firstName>John</firstName> <lastName>Smith</lastName> <addressLine>100 main st.</addressLine> <addressLine>Suite 100</addressLine> <addressLine /> <city>RTP</city> <state>North Carolina</state> <country>United States</country> <zipCode>27560</zipCode> <phone1>919-111-1111</phone1> <email1>u1@m.com</email1> </root>
Example: A map with a nested list:
- JSON
{ "orderItem": [ { "productId": "10541", "quantity": "2.0", "itemAttributes": [ { "attrName": "10299", "attrValue": "4T" } ] }, { "productId": "10260", "quantity": "1.0" } ] }
- XML
<?xml version="1.0" encoding="UTF-8" ?> <root> <orderItem> <productId>10541</productId> <quantity>2.0</quantity> <itemAttributes> <attrName>10299</attrName> <attrValue>4T</attrValue> </itemAttributes> </orderItem> <orderItem> <productId>10260</productId> <quantity>1.0</quantity> </orderItem> </root>
The serializeMapToXML utility
- java.lang.String serializeMapToXML(java.util.Map<?,?> mapRef)
- This method takes a Map and returns an XML representation of it.
This method assigns a top level element, called
root
, for the XML structure. - java.lang.String serializeMapToXML(java.util.Map<?,?> mapRef, Boolean unformatted)
- This method performs the same Map conversion, except the XML is returned unformatted if the Boolean parameter is set to true.