SpringBoot doesn't handle org.hibernate.exception.ConstraintViolationException

后端 未结 11 950
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 01:37

I have defined a pattern for validating email in my Entity class. In my validation exception handler class, I have added handler for ConstraintViolationException. My appli

11条回答
  •  悲哀的现实
    2020-12-10 02:06

    You can handle org.hibernate.exception.ConstraintViolationException by adding this in your @controllerAdvice

    @ExceptionHandler(DataIntegrityViolationException.class) public ResponseEntity handleConstraintViolationException(Exception ex){

        String errorMessage = ex.getMessage();
        errorMessage = (null == errorMessage) ? "Internal Server Error" : errorMessage;
    
        List details = new ArrayList<>();
         details.add(ex.getLocalizedMessage());
    
        return new ResponseEntity(
                new ErrorResponseDTO( errorMessage ,details), HttpStatus.INTERNAL_SERVER_ERROR);
    
    }
    

提交回复
热议问题