Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExceptionHandler shared by multiple controllers

Is it possible to declare ExceptionHandlers in a class and use them in more than one controller, because copy-pasting the exception handlers in every controller would be redundant.

-Class declaring the exception handlers:

@ExceptionHandler(IdentifiersNotMatchingException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
def @ResponseBody
String handleIdentifiersNotMatchingException(IdentifiersNotMatchingException e) {
    logger.error("Identifiers Not Matching Error", e)
    return "Identifiers Not Matching Error: " + e.message
}

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
def @ResponseBody
String handleResourceNotFoundException(ResourceNotFoundException e) {
    logger.error("Resource Not Found Error", e)
    return "Resource Not Found Error: " + e.message
}

-ContactController

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@RequestMapping(value = "contact/{publicId}", method = RequestMethod.DELETE)
def @ResponseBody
void deleteContact(@PathVariable("publicId") String publicId) throws ResourceNotFoundException, IdentifiersNotMatchingException {...}

-LendingController

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@RequestMapping(value = "lending/{publicId}", method = RequestMethod.PUT)
def @ResponseBody
void updateLending(@PathVariable("publicId") String publicId, InputStream is) throws ResourceNotFoundException {...}
like image 232
Jihed Amine Avatar asked Sep 26 '11 15:09

Jihed Amine


People also ask

Can we have multiple @ControllerAdvice?

I have multiple classes annotated with @ControllerAdvice , each with an @ExceptionHandler method in. One handles Exception with the intention that if no more specific handler is found, this should be used.

Is it possible to define more than one @ExceptionHandler for the same exception in the same class in spring?

As mentioned earlier, above exception handler will handle all exceptions which are either instance of given class or sub-classes of argument exception. But, if we want to configure @ExceptionHandler for multiple exceptions of different types, then we can specify all such exceptions in form of array.

How does spring boot handle multiple exceptions?

ExceptionHandler. The @ExceptionHandler is an annotation used to handle exceptions thrown during the execution of handlers and send custom responses to the client. The most common way to use @ExceptionHandler is on methods of @ControllerAdvice classes so that the exception handling is applied globally.

What is the difference between ControllerAdvice and RestControllerAdvice?

The differences between @RestControllerAdvice and @ControllerAdvice is : @RestControllerAdvice = @ControllerAdvice + @ResponseBody . - we can use in REST web services. @ControllerAdvice - We can use in both MVC and Rest web services, need to provide the ResponseBody if we use this in Rest web services.


2 Answers

One way to do this is to have a base class that your controllers extend (could be abstract). The base class can then hold all of the "common" things, including exception handlers, as well as loading common model data, such as user data.

like image 140
cdeszaq Avatar answered Sep 30 '22 12:09

cdeszaq


You can declare a HandlerExceptionResolver as a bean which would be used on every controller. You would just check the type and handle it as you wish.

like image 36
smp7d Avatar answered Sep 30 '22 12:09

smp7d