Camel rest dsl json contains escape sequence

2019-08-08 13:11发布

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

2条回答
放我归山
2楼-- · 2019-08-08 13:41

Add line in Processor:

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

This is workaround.

查看更多
放我归山
3楼-- · 2019-08-08 13:52

My guess is that it's due to the toString at the end of this line:

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

Try removing toString and see how you get on.

And I won't even comment on how questionable an idea it to roll your own security ;-)

查看更多
登录 后发表回答