本文介绍了骆驼其余的DSL JSON包含转义序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要骆驼的帮助.我准备了一些休息服务,但是我的回应有一个小问题.我的回复在之前包含转义序列.有人可以帮助我解决这个问题吗?

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?

我的配置:


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");

这是解决方法.

这篇关于骆驼其余的DSL JSON包含转义序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 23:21