【问题标题】:Apache Camel - pass on Error 400 json instead of throwing its own exceptionApache Camel - 传递错误 400 json 而不是抛出自己的异常
【发布时间】:2021-10-11 19:36:44
【问题描述】:

我正在使用 Apache Camel 来公开一个内部 REST 端点。 内部端点返回代码 400(错误请求)以及带有错误详细信息的有用 JSON。

默认情况下,Camel 通过返回代码 500 做出反应。如何让 Camel 传递代码 400 及其随附的 JSON 正文?

骆驼路线:

    rest("/internal")
            .post("/getServiceState")
            .to("direct:getServiceState");

    from("direct:getServiceState")
            .to("http://endpoint/getServiceState" + "?bridgeEndpoint=true");

【问题讨论】:

标签: java error-handling apache-camel


【解决方案1】:

使用 okStatusCodeRange 参数,您可以像应用程序没有收到错误一样进行开发。

.to("http://endpoint/getServiceState" + "?bridgeEndpoint=true&okStatusCodeRange=200-299,400-501").process(e-> {// do something })

另外,你可以使用cbr根据传入的状态码的状态来采取行动。

.choice().when().simple("${header.CamelHttpResponseCode} == '400'")
            .unmarshal().json(MigrosErrorResponse.class).otherwise().process(e-> {})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多