使用spring-data-rest来公开我想要覆盖默认异常处理的存储库。
阅读文档,我认为最好的wat将使用@ControllerAdvice注释类
@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;
}
}
有几点我不确定:
顺便说一下,即使我尝试了不同的配置,这似乎也不起作用。有没有办法在spring-data-rest中自定义错误处理?
答案 0 :(得分:0)
它缺少RepositoryRestExceptionHandler。会是这样的:
像这样:
@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;
}
}