1

So here is my requirement to handle component/package-specific exceptions. for example for package A, BAD Request should return spring default bad request-response, but for package B it should return custom response.

Yogeen Loriya
  • 589
  • 2
  • 5

2 Answers2

2

You can specify the handled controller by this way:

@ControllerAdvice(assignableTypes = {MyController.class})
public class CoreExceptionHandler {
 ....
 ....
 ....
}
Muhammed_G
  • 375
  • 3
  • 7
1

You can specify the behaviour for specific exceptions thrown from your code using an @ExceptionHandler. Also, you can limit your exception handlers to specific packages:

@ControllerAdvice(package = "com.example.b")
public class PackageBErrorHandler {

    @ResponseStatus(BAD_REQUEST)
    @ExceptionHandler
    @ResponseBody
    public ErrorDto handleValidationError(ConstraintValidationException e) {
        return ...; //build your custom response here
    }
}
crizzis
  • 9,978
  • 2
  • 28
  • 47