cancel
Showing results for 
Search instead for 
Did you mean: 

Java mapping error

Former Member
0 Kudos

I have a synchronous REST -  ECC scenario. I am using REST sender adapter.

In the response message, i am using a java mapping to convert xml to JSON. I am getting this error

does not implement the required interface com.sap.aii.mapping.api.StreamTransformation or does not enhance the class com.sap.aii.mapping.api.AbstractTransformation:

I downloaded a jar file json-org.jar. I used this as imported archive in ESR and added this under operation mapping for response

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Midhun,

                   Can you share the code you wrote?

The error simply means PI is not finding the required standard java mapping classes.

Regards

Anupam

Former Member
0 Kudos

Anupam

I didn't write any code. I just used a jar file that I found online for converting to JSON.

I get it now. I have to use some classes.

I am on single stack PO 7.4 SP 12. which version of NWDS should I use to write code?

Also, I have eclipse in my machine so is it enough if I code in eclipse

former_member182412
Active Contributor
0 Kudos

Hi Midhun,

Use below java mapping.


import java.io.InputStream;

import java.io.OutputStream;

import org.json.JSONObject;

import org.json.XML;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

public class XMLToJSONJavaMap extends AbstractTransformation {

  @Override

  public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)

  throws StreamTransformationException {

  InputStream inputStream = transformationInput.getInputPayload().getInputStream();

  OutputStream outputStream = transformationOutput.getOutputPayload().getOutputStream();

  try {

  byte[] buf = new byte[inputStream.available()];

  inputStream.read(buf);

  JSONObject xmlJsonObj = XML.toJSONObject(new String(buf, "utf-8"));

  String jsonPrettyPrintString = xmlJsonObj.toString(4);

  byte[] bytes = jsonPrettyPrintString.toString().getBytes("UTF-8");

  outputStream.write(bytes);

  } catch (Exception e) {

  getTrace().addDebugMessage("Exception while writing OutputPayload: IOException", e);

  throw new StreamTransformationException(e.toString());

  }

  }

}

Regards,

Praveen.

Former Member
0 Kudos

Thanks a million for the code Praveen. Hope this takes care of unbounded rows as well.

I am downloading NWDS and it's taking time. When I create a class in NWDS should I use the same name that used? XMLToJSONJavaMap

engswee
Active Contributor

Hi Midhun

Having a copy of NWDS is good and I'd definitely recommend using it for your development. However, at >1GB it does take a while to download and it can sometimes be a bit "tricky" to set up correctly.

While you wait for it to download, in the meantime you can check out a "trick" on how you can in order to test out Praveen's code.

Rgds

Eng Swee

former_member182412
Active Contributor
0 Kudos

Hi Midhun,

you can use the same name XMLToJSONJavaMap for your java mapping program.

I have tested it and it works for unbounded.

Remember to import org.json jar as imported archive in repository.

Regards,

Praveen.

Former Member
0 Kudos

Praveen

The code tat you gave me, should I develop it in NWDS and add as a jar under imported archives or should I create it as a mapping in esr in the existing response mapping like how mentioned here. Write java mapping directly in ESR?

If I am importing jar file from NWDS then I caanot import org.json jar to imported archives right?

I have onluy done udfs and not well versed with java mappings..

former_member182412
Active Contributor
0 Kudos

Hi Midhun,

You can write it in NWDS and import it as jar file, you can import org.json jar file as separate imported archive in the same namespace which you are imported java mapping.

Regards,

Praveen.

Former Member
0 Kudos

Hi Praveen

I got this error when I triggered from POSTMAN

Error encountered while executing mapping: com.sap.aii.af.service.mapping.MappingException: Unexpected exception caught while executing mapping: javax.ejb.EJBException: nested exception is: java.lang.RuntimeException: java.lang.Error: Unresolved compilation problems:

  The import org.json cannot be resolved

  The import org.json cannot be resolved

  JSONObject cannot be resolved to a type

  XML cannot be resolved

I coded in NWDS.

I have attached screenshot of both

manoj_khavatkopp
Active Contributor
0 Kudos

Hi Midhun,


The error is because you havent imported json jar , you have just mentioned import statment in jav mapping.


As praveen mentioned you need to import json jar :


you can download json jar from here :


org.json-20120521.jar - org-json-java - org.json-20120521.jar - org.json.jar for Java from www.json.org - Google Pro…


Once downloaded add into your project in eclipse / nwds then compile the java mapping provided by praven and create a jar for that.

Then finally in ESR under imported archive you need to import json jar and the jar which you created after compiling java mapping.

Br,

Manoj

Former Member
0 Kudos

Thanks Manoj for the response.

I have attached the jar, in fact the very same jar that you sugested. Please check my first screen shot in the last message. I have very much attached both jars

Former Member
0 Kudos

I imported json.org jar to NWDS, then compiled again. Exported the mapping as new jar file. Then imported both jar files, json.org jar file and mapping jar file to ESR and it's working.

my mistake was I made a jar file of the code with errors.

Former Member
0 Kudos

Praveen

this code is working fine, thanks a lot man

engswee
Active Contributor
0 Kudos

Hi Midhun

Now that your issue with the backslash is resolved. Please do take some time to close the other threads that you have also opened for the same issue.

Rgds

Eng Swee

Former Member
0 Kudos

Hi Eng

Certainly   I always close threads.

The code is working fine but I was just waiting confirmation from the business people that it's working at their end also. As it stands, they are getting the proper JSON without backward slash

nitindeshpande
Active Contributor
0 Kudos

Hi Midhun,

For SP12, you need to use the latest SP version of 7.31, which is NWDS 7.31 SP 17.

Regards,

Nitin

Answers (1)

Answers (1)

vadimklimov
Active Contributor
0 Kudos

Hi Midhun,

Why do you need to convert XML to JSON in mapping - why not to utilize standard XML/JSON conversion functionality of a REST adapter?

Regarding an error you get. Library that you imported is a JSON processor, not a valid mapping. So you need to develop your custom mapping program and you may get use of that JSON library in a mapping program if necessary, but you cannot directly refer to JSON library in an operation mapping. In operation mapping, you can only refer to Java classes that extend abstract class com.sap.aii.mapping.api.AbstractTransformation.

Regards,

Vadim

Former Member
0 Kudos

Hi vadim

I tried the existing conversion, I am getting this error.

I am sending a url in the payload in the response message from ECC.

It gets escaped with a backward slash when I do that.

the REST webservice is getting the mesage like this:

http://host:port/REST/DFT/employee/<number> in the payload will become like this

http:\/\/host:port\/REST\/DFT\/employee\/<number>


This is how the REST channel is doing converison.


I have only two options. 1) to use adapter module to remove back slash from target payload or 2) use java mapping to convert xml to json so that forward slashes in url will not be escaped

vadimklimov
Active Contributor
0 Kudos

Midhun,

Escaping of forward slash is default behaviour of Jettison processor which is used by a REST adapter. Do you have possibility to unescape it on sender side when processing the received response?

Regards,

Vadim

Former Member
0 Kudos

Vadim

Unfortunately, no, they don't have any provison to do that. I have no choice but give the JSON format output withpout forward slash.

vadimklimov
Active Contributor

Midhun,

Jettison has parameterization to switch on/off escaping of forward slash which can be set through its configuration, but looking in REST adapter, it doesn't look like it implements support of this configuration option currently. So maybe you can raise a suggestion to SAP to implement this.

For PO 7.4, an appropriate NWDS version is 7.31. As for SP level of NWDS, following SAP recommendation, "Install SAP NetWeaver Developer Studio (NWDS) 7.3 EHP1 latest SP to use it as development environment for SAP NetWeaver 7.4 latest SP. If you are using SAP NetWeaver 7.4 SP lower that the latest, please install SAP NetWeaver Developer Studio 7.3 EHP1 with SP version five levels higher than the number of your SAP NetWeaver 7.4 SP." (extract from SAP Note 1791485). In your case, NWDS 7.31 with currently latest SP is applicable.

Yes, you can use pure Eclipse installation for Java mappings development. Having NWDS in place simplifies construction of build path of a Java mapping project (necessary mapping API libraries are already there and can be referenced, no need to download specific JAR files from application server), but if you already have pure Eclipse in place and if you are fine with constructing build path manually, then pure Eclipse is absolutely fine for development of Java mapping programs.

Regards,

Vadim