在Apache Camel API中接收空错误响应

发布于 2025-02-10 06:31:11 字数 1623 浏览 0 评论 0原文

我是Apache骆驼的新手。我的要求是,每当发生自定义时,我都想设置自定义主体和响应代码,并将其作为响应发送给用户。 我已经使用Onexception块来捕获自定义例外:

onException(CustomException.class)
                .handled(true)
                .to("direct:customErrorHandler");

我的CustomErrorhandler是不同类“ customErrorhandler”的路由: -


@Component
public class CustomExceptionHandler extends RouteBuilder {
    
    @Autowired
    private CommonExceptionProcessor commonExceptionProcessor;

    @Override
    public void configure() throws Exception {

        JacksonDataFormat jackErrorResponse = new JacksonDataFormat(jacksonConfig.jsonObjectMapper(), Error.class);

        from("direct:customErrorHandler")
                .routeId("customErrorHandler-Route")
                .log(LoggingLevel.DEBUG, this.getClass().getName(), "Handling Error")
                .process(commonExceptionProcessor)
                .marshal(jackErrorResponse)
           }
}

我的常见型号处理器: -

@Component
public class CommonExceptionProcessor implements Processor {
    
    @Override
    public void process(Exchange exchange) throws Exception {


       CustomException ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, CustomException.class);
        Error e = new Error();
        e.setMessage(ex.getMessage());
        e.setStatus("FAILED");
        exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, ex.getCode());
        exchange.getIn().setBody(e,Error.class);
        }
}

但是,每当发生此例外,我都会使用垃圾范围的状态代码空白响应。请帮忙。

期望的回应:

{
"message":"customException occured",
"status":"FAILED"
}

I am new to Apache Camel. My requirement is that, whenever a custom exception occur, i want to set a custom Body and response code and send it back to the user as response.
I have used OnException Block to catch the custom exception :

onException(CustomException.class)
                .handled(true)
                .to("direct:customErrorHandler");

My customErrorHandler is a route in a different class "CustomErrorhandler":-


@Component
public class CustomExceptionHandler extends RouteBuilder {
    
    @Autowired
    private CommonExceptionProcessor commonExceptionProcessor;

    @Override
    public void configure() throws Exception {

        JacksonDataFormat jackErrorResponse = new JacksonDataFormat(jacksonConfig.jsonObjectMapper(), Error.class);

        from("direct:customErrorHandler")
                .routeId("customErrorHandler-Route")
                .log(LoggingLevel.DEBUG, this.getClass().getName(), "Handling Error")
                .process(commonExceptionProcessor)
                .marshal(jackErrorResponse)
           }
}

My Common expection processor:-

@Component
public class CommonExceptionProcessor implements Processor {
    
    @Override
    public void process(Exchange exchange) throws Exception {


       CustomException ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, CustomException.class);
        Error e = new Error();
        e.setMessage(ex.getMessage());
        e.setStatus("FAILED");
        exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, ex.getCode());
        exchange.getIn().setBody(e,Error.class);
        }
}

But whenever this exception is occuring, i am getting empty response with garbage statusCode. please help.

Expectated Response:

{
"message":"customException occured",
"status":"FAILED"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文