簡體   English   中英

駱駝其余的DSL JSON包含轉義序列

[英]Camel rest dsl json contains escape sequence

我需要駱駝的幫助。 我准備了一些休息服務,但是我的回應有一個小問題。 我的回復在“之前包含轉義序列。有人可以幫助我解決這個問題嗎?

我的配置:


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 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>

日志:

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\"}}"

在處理器中添加行:

exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "text/plain");

這是解決方法。

我的猜測是這是由於此行末尾的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);

我的推理: restConfiguration表示它將返回JSON。 您構造了一個JSONObject但隨后將其轉換為字符串,因此路由認為您想返回一個字符串,而不是對象,因此轉義所有引號以使其成為有效的JSON字符串。

嘗試刪除toString ,看看如何進行。

而且,我什至不會評論推銷自己的安全性的想法有多可疑;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM