开发者

ExceptionHandler shared by multiple controllers

开发者 https://www.devze.com 2023-04-07 10:00 出处:网络
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.

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


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.


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.


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


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
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消