【问题标题】:Camel rest route not returning status code on exception骆驼休息路线不返回异常状态码
【发布时间】:2018-04-06 07:02:49
【问题描述】:

我创建了一个骆驼路线,具有以下异常处理:

onException(BadRequestException.class)
    .handled(true)
    .process(exchange -> {
        System.out.println("Reached processor");
        System.out.println(exchange.getIn().getBody(String.class));
    })
    .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(HttpStatus.BAD_REQUEST));

我向http://localhost:8080/services/rest/endpoint?key=value提出请求

在 bean 中,我有一些验证表明需要两个查询参数,keykeyTwokeyTwo 不存在,所以我抛出一个BadRequestException

public void assertRequiredParametersPresentOnExchange() throws BadRequestException {
    try {
        requiredParameters.stream()
            .forEach(p -> assertNotNull(p));
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage());
    }
}

当我执行 URL 时,我可以看到上面异常处理程序的 Reached processor 输出,但是什么也没发生。 Postman 等待回复约 60 秒,然后给我状态码 23。

我在这里缺少什么?环保部?在交易所设置一些属性?

【问题讨论】:

  • 我通常在exception handlers 调用的own bean 中处理HTTP 错误响应,其中我将msg.setFault(false) 明确设置为Camel,否则Jetty 会用a 覆盖我定义的状态代码500个。不确定您使用的是哪种服务器实现
  • 谢谢,我在更改状态码时考虑到了这一点。处理器主要放置在那里以追踪问题发生的位置。它现在被处理并格式化为一个通用的 json 错误正文,类似于您的链接!

标签: java apache-camel


【解决方案1】:

似乎你得到的枚举序号是 23。你能尝试替换枚举

org.apache.http.HttpStatus.SC_BAD_REQUEST 

而不是

org.springframework.http.HttpStatus.BAD_REQUEST

看看有没有区别

【讨论】:

  • 假设您在示例中使用 org.springframework.http.HttpStatus.BAD_REQUEST
猜你喜欢
  • 2013-12-04
  • 1970-01-01
  • 2013-08-13
  • 2023-04-06
  • 2023-01-31
  • 2018-09-05
  • 2020-05-13
  • 2019-06-29
  • 1970-01-01
相关资源
最近更新 更多