Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ControllerAdvice from external jar

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

like image 675
naveen singh Avatar asked Nov 07 '22 20:11

naveen singh


1 Answers

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.

like image 56
Velayudham K Avatar answered Dec 05 '22 03:12

Velayudham K