首页 > 解决方案 > Object not returned to caller as expected during exception in camel route

问题描述

I am getting a NullPointerException in a bean while processing my route. Below is the format of my exception handler:

public class BaseRB extends RouteBuilder {

     super.configure();
    
     onException(Exception.class)
                .logStackTrace(true)
                .bean(ExceptionLoggingProcessor.class)
                .bean(ResponseBuilder.class,
                        "buildErrorResponse")
                .handled(true);
}

The Exception logging Processor is a processor class that simply handles exception logging. The buildErrorResponse in the ResponseBuilder class returns an object of type org.springframework.http.ResponseEntity.

        //Error Response is the custom error object
        ErrorResponse errorResponse = new ErrorResponse(
                HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
                HttpStatus.INTERNAL_SERVER_ERROR.value(),
                HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()
        );
        return ResponseEntity.
                status(HttpStatus.INTERNAL_SERVER_ERROR)
                .contentType(MediaType.APPLICATION_JSON)
                .body(errorResponse);

The route is being invoked from a controller class using producerTemplate as below:

producerTemplate.requestBodyAndHeaders(ApplicationConstants.DIRECT_MAIN_ROUTE, 
                order, headers, ResponseEntity.class);

The route definition looks like below:

public class MainRB extends BaseRB {

   super.configure();

   from("direct:mainRoute")
         .to("direct:firstRoute")
         .to("direct:secondRoute")
         .bean(ResponseBuilder.class,
                        "buildSuccessResponse")
         .end();
}

public class FirstRB extends BaseRB {

   super.configure();

   from("direct:firstRoute")
         .bean(ExchangeProcessingBean.class, "doProcessing")
         .end();
}

public class ExchangeProcessingBean {
     // The method from where I am getting the NPE
    public void doProcessing(Exchange exchange) {
        //do processing 
    }

}

There is one base RouteBuilder where the onException is defined. This is extended by other routes.

The expected behavior is that when an exception is thrown in any of the routes response entity should be returned to the controller class from the ResponseBuilder class. In this scenario, a NPE is being thrown from the doProcessing method. The onException is executed and ResponseBuilder method 'buildExceptionErrorResponse' is also executed. But instead of returning the ResponseEntity to the controller, camel proceeds to execute the next route.

标签: spring-booterror-handlingapache-camel

解决方案


推荐阅读