骆驼路线-
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());
和后处理器类-
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());
}
返回客户端时,示例结果在JSON数组/对象中包含转义序列
"{\"result\":[{\"parent\":\"\",\"made_sla\":\"true\",\"caused_by\":\"\"
如果需要,请帮助修改restlet配置,以便在JSON响应中获得响应而不使用转义序列。