【问题标题】:Set HTTP code onException with Camel and Restlet使用 Camel 和 Restlet 设置 HTTP 代码 onException
【发布时间】:2013-06-10 13:59:05
【问题描述】:

在 Apache Camel 项目中使用 Restlet component,我无法在 onException 的响应中设置 HTTP 状态代码。

考虑一个配置如下的骆驼RouteBuilder

onException(Exception.class)
  .handled(true)
  .process(new FailureResponseProcessor());

from("restlet:http://example.com/foo")
  .process(fooProcessor)
  .to("file:data/outbox");

FailureResponseProcessor我尝试设置Restlet的HTTP响应码:

public void process(Exchange exchange) throws Exception {
  Response response = exchange.getIn()
                              .getHeader(RestletConstants.RESTLET_RESPONSE,
                                         Response.class);
    response.setStatus(Status.SERVER_ERROR_INTERNAL);
}

这不起作用。 HTTP 响应的状态始终为200 OK

什么是有效设置 HTTP 状态码的必要条件?

【问题讨论】:

    标签: exception apache-camel restlet http-status-codes


    【解决方案1】:

    在这里,您只是检索响应对象并对其进行更改。这不会沿路线传播。修改对象后需要设置header。

    exchange.getIn().setHeader(RestletConstants.RESTLET_RESPONSE, response);
    

    谢谢。

    【讨论】:

    • 这帮助我找到了正确的方向。实际的问题是在上游处理器中没有传播的标头。
    【解决方案2】:

    你可以通过设置强制你的异常状态码

    .onException(MyException.class).process(SetFailureResponse::new).handled(true)
    

    并在一个单独的 SetFailureResponse 类集合中

    @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE,400);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-21
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      相关资源
      最近更新 更多