处理Spring Hibernate SQL错误的推荐方法

处理Spring Hibernate SQL错误的推荐方法,第1张

处理Spring Hibernate SQL错误的推荐方法

您可以定义自己的错误处理程序,该错误处理程序扩展了 ResponseEntityExceptionHandler
并捕获特定于数据库的异常。这是一个代码…

@RestControllerAdvice@RequestMapping(produces = "application/json")public class DefaultExceptionHandler extends ResponseEntityExceptionHandler {    // This method handles constraint violation exception raised by DB.     // Similarly other type exceptions like custom exception and HTTP status related     //exception can be handled here.    @ExceptionHandler(ConstraintViolationException.class)    @ResponseStatus(value = HttpStatus.CONFLICT)    public Map<String, String> handleConstraintViolationException(ConstraintViolationException ex) {    // write your own logic to return user friendly response    }   // Below method is to handle _SqlExceptionHelper_ exception    @ExceptionHandler(SqlExceptionHelper.class)    @ResponseStatus(value = HttpStatus.CONFLICT)    public Map<String, String> handleConstraintViolationException(SqlExceptionHelper ex) {    // write your own logic to return user friendly response    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5674020.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存