gpt4 book ai didi

java - SpringBoot 不处理 org.hibernate.exception.ConstraintViolationException

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:12:46 32 4
gpt4 key购买 nike

我已经在我的实体类中定义了一个验证电子邮件的模式。在我的验证异常处理程序类中,我添加了 ConstraintViolationException 的处理程序。我的应用程序使用 SpringBoot 1.4.5。

配置文件.java

@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "profile")
public class Profile extends AuditableEntity {

private static final long serialVersionUID = 8744243251433626827L;

@Column(name = "email", nullable = true, length = 250)
@NotNull
@Pattern(regexp = "^([^ @])+@([^ \\.@]+\\.)+([^ \\.@])+$")
@Size(max = 250)
private String email;
....
}

ValidationExceptionHandler.java

@ControllerAdvice
public class ValidationExceptionHandler extends ResponseEntityExceptionHandler {

private MessageSource messageSource;

@Autowired
public ValidationExceptionHandler(MessageSource messageSource) {
this.messageSource = messageSource;
}

@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex,
WebRequest request) {
List<String> errors = new ArrayList<String>();
....
}
}

当我运行代码并传递无效的电子邮件地址时,出现以下异常。 handleConstraintViolation 中的代码永远不会执行。异常中返回的 http 状态是 500,但我想返回 400。知道如何实现吗?

2017-07-12 22:15:07.078 ERROR 55627 --- [nio-9000-exec-2] o.h.c.s.u.c.UserProfileController        : Validation failed for classes [org.xxxx.common.service.user.domain.Profile] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='must match "^([^ @])+@([^ \.@]+\.)+([^ \.@])+$"', propertyPath=email, rootBeanClass=class org.xxxx.common.service.user.domain.Profile, messageTemplate='{javax.validation.constraints.Pattern.message}'}]

javax.validation.ConstraintViolationException: Validation failed for classes [org.xxxx.common.service.user.domain.Profile] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='must match "^([^ @])+@([^ \.@]+\.)+([^ \.@])+$"', propertyPath=email, rootBeanClass=class org.xxxx.common.service.user.domain.Profile, messageTemplate='{javax.validation.constraints.Pattern.message}'}]

at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:138)

at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:78)

最佳答案

您无法捕获 ConstraintViolationException.class,因为它不会传播到代码的那一层,它会被较低的层捕获、包装并在另一种类型下重新抛出。因此,命中您的 Web 层的异常不是 ConstraintViolationException

在我的例子中,它是一个 TransactionSystemException。我正在使用来自 Spring 的 @Transactional 注释和 JpaTransactionManager。当事务中出现问题时,EntityManager 抛出回滚异常,由 JpaTransactionManager 将其转换为 TransactionSystemException

所以你可以这样做:

@ExceptionHandler({ TransactionSystemException.class })
public ResponseEntity<RestResponseErrorMessage> handleConstraintViolation(Exception ex, WebRequest request) {
Throwable cause = ((TransactionSystemException) ex).getRootCause();
if (cause instanceof ConstraintViolationException) {
Set<ConstraintViolation<?>> constraintViolations = ((ConstraintViolationException) cause).getConstraintViolations();
// do something here
}
}

关于java - SpringBoot 不处理 org.hibernate.exception.ConstraintViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45070642/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com