on 12-03-2015 2:29 AM
Hi,
I have a sender REST scenario, where my sender is sending the JSON request to SAP PO, I am able to take the request map it to my receiver. When I am getting the response I need to convert the payload from XML to JSON. In this conversion I see an element which I have defined as String in xml in SAP PO that gets converted to integer in JSON response.
{"orderNo":"",
"taxTransactionDocID":"",
"responseCode":500,
"responseMessage":"Please select the address from proposed addressess "
}
Here in my xml I have defined responseCode as String but the value it carries is a number. When the response goes as JSON it shows up as a number.
<xsd:element name="responseCode" type="xsd:string" minOccurs="0"/>
Thanks in advance.
Ravijeet
Hi Ravijeet,
REST adapter uses 3rd party Jettison processor for JSON handling. Behaviour that you described, is a default way how Jettison processes XML and converts it into JSON. To be more precise, Jettison uses so-called type converter in order to make conversion of processed value when producing JSON output. Any value of an element which needs to be processed, is passed to type converter as a string (even if underlying type for the element according to XML document's XSD schema is not string, but integer or something else). Default type converter used by Jettison removes double quotes for values that are numeric.
It is possible to instruct Jettison to use another type converter by setting corresponding value to a Java system property jettison.mapped.typeconverter.class. For example, if you set value of this property to org.codehaus.jettison.mapped.SimpleConverter (default type converter is org.codehaus.jettison.mapped.DefaultConverter), Jettison will produce values surrounded by double quotes in an output. But this will mean that even if XML document contains an element of type integer, its value in JSON output will be surrounded by double quotes, too - which means, all element values will be surrounded by double quotes despite their type (referring to earlier statement that it Jettison doesn't perform check of element type as per XSD schema of the original XML document).
You may also develop and deploy your own type converter class (which should implement an interface org.codehaus.jettison.mapped.TypeConverter, method convertToJSONPrimitive()) and make use of it by Jettison if you need some advanced logic when producing elements values in JSON output.
Java system property can be set/changed using NetWeaver Administrator: Configuration > Infrastructure > Java System Properties > tab "System VM Parameters" (instance restart is needed in order for change to take effect).
Regards,
Vadim
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Vadim,
Thank you for your reply.
I am on SAP PI 7.4 SP 11. I searched in the System VM parameters but could not find the name you suggested. I even searched in the Advanced VM parameters but there also I didn't see this property.
Somebody suggested to use transform bean but this is for received REST adapter whereas mine is on the sender side.
Thanks in advance
Ravijeet
Hi Vadim,
As you had stated earlier this property sets all values as string. I have decimals which now got converted as strings. So I would need to develop a java class and deploy that ?
Also I am facing another problem, that we have an Array-like Element in the Repsonse XML DataType. Depending on the amount of actual elements in the array, the element gets converted to a JSON Array or JSON Object.
E.g. If the element contains exactly 1 item, it is converted to a JSON object. If it contains more than one item, it is converted to an array. if it contains no elements, it is not contained in JSON at all.
Regards
Ravijeet
Hi Ravijeet,
Behaviour that you describe, is specifics of Jettison JSON processor: for example, if the array consists of only one element, most likely Jettison will not recognize it to be an array, but will qualify it as a complex object with a nested structure.
Regarding custom type converter class. It is not a common practice to make such an enhancement, but considering Jettison employs type converter and can be extended with custom classes and REST adapter calls Jettison for XML/JSON conversion, it is necessary to ensure corresponding developed class is deployed and loaded properly, before or at a time of REST adapter invocation.
Regards,
Vadim
Hi Vadim,
You mentioned
You may also develop and deploy your own type converter class (which should implement an interface org.codehaus.jettison.mapped.TypeConverter, method convertToJSONPrimitive()) and make use of it by Jettison if you need some advanced logic when producing elements values in JSON output.
So I would need to write this class but how do I deploy this class ? Or do I go with an adapter module approach ?
I see we can use Moxy for this use case. Java XML and JSON Binding: MOXy as Your JAX-RS JSON Provider - MOXyJsonProvider
Not sure how to implement this.
Can I just use Jackson for the response in the java mapping and instruct the sender rest adapter not to do any xml to json conversion ?
Thanks
Ravijeet
You can try to assemble a developed class into a library and deploy a custom library to an application server.
Using JSON format in response message instead of XML is feasible, but in this case JSON response will be handled by REST adapter as an unstructured text. So if you construct required JSON message in a form that is compliant to both JSON structure and your expectations on earlier stage of message processing in PO, it should work. Give a try and test it - for example, when preparing string with JSON message in a response mapping.
Regards,
Vadim
User | Count |
---|---|
101 | |
13 | |
13 | |
11 | |
11 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.