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 {...}
Jihed Amine
  • 2,198
  • 19
  • 32

4 Answers4

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.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
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.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
smp7d
  • 4,947
  • 2
  • 26
  • 48
  • 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 '11 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... http://stackoverflow.com/questions/7405380/how-do-i-hook-up-a-handlerexceptionresolver – smp7d Sep 26 '11 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. – Jihed Amine Sep 26 '11 at 17:49
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
    }
}
foal
  • 693
  • 7
  • 20
0

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

Harry
  • 4,705
  • 17
  • 73
  • 101