4

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 {...}

4 Answers 4

6

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.

4
  • What's wrong with this answer? Sure, it isn't perhaps as clean as using Spring beans, but it's still a viable option, and there are likely to be a number of cross-controller things that a base controller can take care of.
    – cdeszaq
    Sep 26, 2011 at 16:56
  • It's what I use (not with Spring though) Sep 26, 2011 at 16:58
  • Does this work with annotations. I thought that annotations were not inherited... Jan 28, 2014 at 20:31
  • @JacobDalton No it doesn't.
    – Andi
    Feb 26, 2015 at 13:25
1

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.

3
  • Cool, I didn't know this existed! Do you know of any example code that shows more details of how it can be used?
    – cdeszaq
    Sep 26, 2011 at 15:15
  • It depends on exactly what you are trying to achieve. You can implement it however you need to, but here is a simple example of one Spring provides out of the box... stackoverflow.com/questions/7405380/…
    – smp7d
    Sep 26, 2011 at 15:25
  • The signature of resolveException that I would have to implement doesn't fit my needs. I just declared a base abstract class having the exception handlers. Sep 26, 2011 at 17:49
0

Or you can create an interface for exception handling and inject that in your controller class.

0

Or you can use a class with @ControllerAdvice annotation.

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(IdentifiersNotMatchingException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public 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)
    @ResponseBody
    public String handleResourceNotFoundException(ResourceNotFoundException e) {
        logger.error("Resource Not Found Error", e)
        return "Resource Not Found Error: " + e.message
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.