[英]Camel rest dsl json contains escape sequence
I need help with Camel. 我需要骆驼的帮助。 I prepared some rest service, but I have small problem with response. 我准备了一些休息服务,但是我的回应有一个小问题。 My response contains escape sequence before ". Could anyone help me with this problem? 我的回复在“之前包含转义序列。有人可以帮助我解决这个问题吗?
My configuration: 我的配置:
restConfiguration().port("{{rest_port}}").component("jetty").host("localhost").bindingMode(RestBindingMode.json);
rest("/login").post().bindingMode(RestBindingMode.json).produces("application/json").consumes("application/json").to("direct:login-rest");
from("direct:login-rest")
.choice()
.when(simple("${body[username]} == '{{rest_user}}' and ${body[password]} == '{{rest_password}}'"))
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
String response = new JSONObject().put("Success", true).put("Errors", "").put("Result", new JSONObject().put("token", CURRENT_TOKEN).put("account", new JSONObject().put("guid", "t123123-31231"))).toString(0);
exchange.getOut().setBody(response);
exchange.getOut().setHeaders(exchange.getIn().getHeaders());
}
})
.log("AFTER Processor ${body}")
.otherwise()
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(403));
Route: 路线:
<route id="login" streamCache="true">
<from uri="direct:login"/>
<setHeader headerName="Exchange.HTTP_METHOD">
<constant>POST</constant>
</setHeader>
<setBody>
<simple>
{ "username": "{{rest_user}}", "password": "{{rest_password}}"}
</simple>
</setBody>
<to uri="http4:localhost:{{rest_port}}/login"/>
<log message="====== ${body}"/>
</route>
Logs: 日志:
2018-02-21 13:48:48,950 [tp1100560861-38] INFO route3 - AFTER Processor {"Errors":"","Success":true,"Result":{"account":{"guid":"XXX-XXX"},"token":"c86d2900-2754-48ba-bd8d-84ce4338f362"}}
2018-02-21 13:48:48,954 [0 - timer://foo] INFO login - ====== "{\"Errors\":\"\",\"Success\":true,\"Result\":{\"account\":{\"guid\":\"XXX-XXX\"},\"token\":\"c86d2900-2754-48ba-bd8d-84ce4338f362\"}}"
Add line in Processor: 在处理器中添加行:
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "text/plain");
This is workaround. 这是解决方法。
My guess is that it's due to the toString
at the end of this line: 我的猜测是这是由于此行末尾的toString
:
String response = new JSONObject().put("Success", true).put("Errors", "").put("Result", new JSONObject().put("token", CURRENT_TOKEN).put("account", new JSONObject().put("guid", "t123123-31231"))).toString(0);
My reasoning: restConfiguration
says that it will return JSON. 我的推理: restConfiguration
表示它将返回JSON。 You construct a JSONObject
but then turn it to a string, so the route thinks you want to return a string, not an object, so escapes all the quotes to make it a valid JSON string. 您构造了一个JSONObject
但随后将其转换为字符串,因此路由认为您想返回一个字符串,而不是对象,因此转义所有引号以使其成为有效的JSON字符串。
Try removing toString
and see how you get on. 尝试删除toString
,看看如何进行。
And I won't even comment on how questionable an idea it to roll your own security ;-) 而且,我什至不会评论推销自己的安全性的想法有多可疑;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.