本文整理汇总了Java中org.springframework.dao.DataIntegrityViolationException.getCause方法的典型用法代码示例。如果您正苦于以下问题:Java DataIntegrityViolationException.getCause方法的具体用法?Java DataIntegrityViolationException.getCause怎么用?Java DataIntegrityViolationException.getCause使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.dao.DataIntegrityViolationException
的用法示例。
在下文中一共展示了DataIntegrityViolationException.getCause方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleDataIntegrityViolationException
import org.springframework.dao.DataIntegrityViolationException; //导入方法依赖的package包/类
@Loggable
@ResponseStatus(code = HttpStatus.CONFLICT)
@ExceptionHandler(DataIntegrityViolationException.class)
public ErrorDto handleDataIntegrityViolationException(DataIntegrityViolationException ex) {
if (uniqueConstraintList != null && ex.getCause() != null && ex.getCause() instanceof ConstraintViolationException) {
Optional<DataUniqueConstraint> matchCons = uniqueConstraintList.stream().filter((cons) ->
((ConstraintViolationException) ex.getCause()).getConstraintName().contains(cons.getConstraintName())).findFirst();
if (matchCons.isPresent()) {
return ErrorDto.builder()
.errorCode(ErrorCodes.DATA_VALIDATION)
.error(ValidationErrorDto.builder()
.errorCode("UNIQUE")
.fieldName(Arrays.stream(matchCons.get().getFieldNames())
.map(Object::toString)
.collect(Collectors.joining(", ")))
.build())
.message(ex.getLocalizedMessage())
.build();
}
}
return ErrorDto.builder()
.errorCode(ErrorCodes.UNKNOWN)
.message(ex.getLocalizedMessage())
.build();
}
示例2: handleDataIntegrityViolation
import org.springframework.dao.DataIntegrityViolationException; //导入方法依赖的package包/类
/**
* Handle DataIntegrityViolationException, inspects the cause for different DB causes.
*
* @param ex the DataIntegrityViolationException
* @return the ApiError object
*/
@ExceptionHandler(DataIntegrityViolationException.class)
protected ResponseEntity<Object> handleDataIntegrityViolation(DataIntegrityViolationException ex,
WebRequest request) {
if (ex.getCause() instanceof ConstraintViolationException) {
return buildResponseEntity(new RestApiError(HttpStatus.CONFLICT, "Database error", ex.getCause()));
}
return buildResponseEntity(new RestApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex));
}