I am trying to create exception handling module which could be used by multiple module. So I create jar which has all logic related to handling exception. I am trying to add global controller advice which could catch exception and generate error response .Problem is this controller advice is not getting invoked. It works fine when I write declare this controller advice in individual modules.
ExceptionHandling Module --ExceptionToResponseGenerator MainModule --RestController This doesn't work
ExceptionHandling Module
MainModule --RestController --ExceptionToResponseGenerator This works
What am I doing wrong?
package com.abc.cde.exceptionhandler.handler;
@ControllerAdvice
@Log
public class ExceptionToResponseGenerator extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders httpHeaders, HttpStatus httpStatus, WebRequest webRequest) {
return new ResponseEntity(new ErrorDetails(new Date(), "bad", "bad"), HttpStatus.BAD_REQUEST);
}
Main class
@Import(IncludeExceptionHandling.class)
@SpringBootApplication
public class MainApplication{
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
-----------
IncludeExceptionHandling
@Configuration
@EnableAspectJAutoProxy
public class IncludeExceptionHandling {
@Bean
public ExceptionLoggerPointcut notifyAspect() {
return new ExceptionLoggerPointcut();
}
}
ExceptionLoggerPointcut is aop wrapping exception and rethrowing it
It seems like the issue is with missing @ComponentScan. Please pass your code's package name and the external jar library name to the @ComponentScan annotation so that Spring can register the components and make them available to your App.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With