Camel Route -
rest("/servicenow").post("/{operation}").consumes("application/json").type(Model.class).produces(MediaType.APPLICATION_JSON).to("direct:servicenow");
from("direct:servicenow")
.setHeader("operationSelector", simple("${header.operation}"))
.process(new PreProcessor())
.recipientList().simple("servicenow://${header.instance}?userName=${header.name}&password=${header.password}&apiUrl=${header.apiUrl}")
.process(new PostProcessor());
and PostProcessor Class -
Object msg = exchange.getIn().getBody();
GsonBuilder builder = new GsonBuilder();
builder.setLenient();
Gson gson = builder.create();
JsonElement element = gson.toJsonTree(msg);
JsonObject result = new JsonObject();
if (element.isJsonArray()) {
JsonArray array = (JsonArray) element;
result.add("result", array);
System.out.println(result.toString());
exchange.getOut().setBody(result.toString());
} else {
result.add("result", element);
System.out.println(result.toString());
exchange.getOut().setBody(result.toString());
}
Example result contains escape sequences in the JSON Array/Object while returning to the Client
"{\"result\":[{\"parent\":\"\",\"made_sla\":\"true\",\"caused_by\":\"\"
Please do help to modify in the restlet configuration if required so I get response without escape sequence in JSON response.
Instead of using this line :
JsonObject result = new JsonObject();
Use this prefered answer :
Map<DataType,DataType> result = new HashMap<DataType,DataType>();
result.put("result", array);
exchange.getOut().setBody(result);
The restlet will bind the map object to a jsonObject.