1

I am trying to limit my Controller Advice handling to only a specific package's subpackages (preferably) or a list of specific classes (I will have to manually add all classes manually this way, so it's not nearly as good). How would I go about doing this? My current advice class is like this:

@RestControllerAdvice
@Slf4j
@AllArgsConstructor
public class ExceptionHandler {
        
    //This exception should run in the entire spring project
    @ExceptionHandler({ MissingRequestHeaderException.class, BindException.class})
    public final ResponseEntity<ErrorDTO> exceptionBindRequestHandler(Exception ex) {
         //.. do whatever
    }

    //This exception should be only run in subpackages of com.example.subpackage1.*, for example
    @ExceptionHandler({ ConnectException.class })
    public final ResponseEntity<ErrorDTO> connectExceptionHandler(Exception ex) {
        //do whatever
    }
}

How would I limit the scope of controller advice on the 2nd @ExceptionHandler? Thanks!

Toni
  • 3,296
  • 2
  • 13
  • 34
Jason Chan
  • 193
  • 1
  • 3
  • 11

0 Answers0