Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
I have created a custom thing template which contains a service which takes data in json string, then generates a PDF report ans save it in file repository.
The issue is, when I hard code the json string inside the service, the custom extension works great. It creates report and save it to repository, But if the same json string input is passed dynamically from imported extension, it gives me the below error:
Unable to Invoke Service CreatePDFReport on PDFThing : java.lang.String cannot be cast to org.json.simple.JSONObject .
JSON String :: "{ \"Date\":\"121212\", \"Shift\":\"One\", \"TeamMembers\":\"Sachin\" }"
Please Help.
----------------------------------------------------------------------------------------------------------------------------------
Below is my JAVA code:
@ThingworxServiceDefinition(name = "CreatePDFReport", description = "", category = "", isAllowOverride = false, aspects = {
"isAsync:false" })
@ThingworxServiceResult(name = "Result", description = "", baseType = "STRING", aspects = {})
public String CreatePDFReport(
@ThingworxServiceParameter(name = "jsonStringA", description = "", baseType = "STRING", aspects = {
"isRequired:true" }) String jsonStringA) throws IOException, DocumentException, JSONException, ParseException {
_logger.trace("Entering Service: CreatePDFReport");
String DEST = "C:\\ThingworxStorage\\repository\\Test_PDF\\PDFReport.pdf";
String jsonStringTest = "{ \"Date\":\"121212\", \"Shift\":\"One\", \"TeamMembers\":\"Sachin\" }";
JSONParser parser = new JSONParser();
JSONObject myData = (JSONObject) parser.parse(jsonStringA);
//JSONObject myData = (JSONObject) parser.parse(jsonStringTest);
new PDFBoxAtom().createPdfAtom(DEST, myData); // Function which creates PDF report and save it to location DEST.
_logger.trace("Exiting Service: CreatePDFReport");
return "PDF Created";
}
Note: If I use jsonStringTest to create PDF report, it works fine But when I pass the same json string dynamically(jsonStringA), it gives me the error.
What does the java code look like for your CreatePDFReport method? Also, what does your method signature look like where you declare your JSON input param? If you post your code, someone will be able to help you.
I have edited the post with my java code.Please have a look
Change your base type from String to JSON for your input param.
Hi @SachinTanwar.
If the solution provided by @wposner-2 allowed you to solve your issue, please mark it as the Accepted Solution for the benefit of others on the Community.
Regards.
--Sharon