Camel rest dsl json 包含转义序列 [英] Camel rest dsl json contains escape sequence

查看:34
本文介绍了Camel rest dsl json 包含转义序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的配置:

<预><代码>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");从(直接:登录休息").选择().when(simple("${body[username]} == '{{rest_user}}' and ${body[password]} == '{{rest_password}}'")).process(新处理器(){@覆盖public void process(Exchange exchange) 抛出异常 {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("处理器后 ${body}").除此以外().setHeader(Exchange.HTTP_RESPONSE_CODE, 常量(403));

路线:

<from uri="direct:login"/><setHeader headerName="Exchange.HTTP_METHOD"><常数>POST</常数></setHeader><setBody><简单>{用户名":{{rest_user}}",密码":{{rest_password}}"}</简单></setBody><to uri="http4:localhost:{{rest_port}}/login"/><log message="====== ${body}"/></路线>

日志:

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

这是解决方法.

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.

这篇关于Camel rest dsl json 包含转义序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆