问题
Using spring-data-rest to expose repositories i want to overwrite the default exception handling.
reading documentation it looks to me that the best wat would be using a @ControllerAdvice annotated class
@ControllerAdvice
class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler {
Logger log = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> badRequest(HttpServletRequest req, Exception exception) {
log.info("++++ GLOBAL EXCEPTION HANDLING ++++");
return null;
}
}
There are several point i am not sure about:
- Should i extend ResponseEntityExceptionHandler, other class, nothing?
- Should i return ResponseEntity object, other class?
By the way this does not seems to work even when i've tried different configurations. Is there a way to customize error handling in spring-data-rest?
回答1:
It's missing RepositoryRestExceptionHandler. Would be something like this:
Like this:
@ControllerAdvice(basePackageClasses = RepositoryRestExceptionHandler.class)
public class GlobalControllerExceptionHandler {
Logger log = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> badRequest(HttpServletRequest req, Exception exception) {
log.info("++++ GLOBAL EXCEPTION HANDLING ++++");
return null;
}
}
来源:https://stackoverflow.com/questions/29331770/global-exception-handling-for-rest-exposed-spring-data