您可以定义自己的错误处理程序,该错误处理程序扩展了 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 }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)