我正在Spring Boot项目中使用休眠验证器验证实体。我有一个日期验证器@Past的异常。我遇到了javax.validation.ValidationException: HV000028: Unexpected exception during isValid call
这是我在gradle.build文件中具有的依赖项
implementation 'org.springframework.boot:spring-boot-starter-validation'
以下是我拥有的类似实体
@NoArgsConstructor
@Data
public class Person{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "FIRST_NAME")
@NotEmpty(message = "ENTER REQUIRED FIELDS - FIRST NAME")
private String firstName;
@Column(name = "LAST_NAME")
@NotEmpty(message = "ENTER REQUIRED FIELDS - LAST NAME")
private String lastName;
@Column(name = "BIRTH_DATE")
@Past(message = "PLEASE ENTER A VALID DATE ")
private Date birthDate;
}
这是日志跟踪
2020-10-25 13:09:11.421 DEBUG 21952 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Completed 500 INTERNAL_SERVER_ERROR```
答案 0 :(得分:0)
我迅速在我的示例项目上运行,其中所有内容都已配置,我认为日期格式是问题所在。可用的默认格式为
这是我的做法
控制器
@PostMapping(value = "test-person")
public TestPerson testPerson(@Valid @RequestBody TestPerson testPerson) {
return testPerson;
}
@Getter
@Setter
@NoArgsConstructor
@Data
public static class TestPerson {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "FIRST_NAME")
@NotEmpty(message = "ENTER REQUIRED FIELDS - FIRST NAME")
private String firstName;
@Column(name = "LAST_NAME")
@NotEmpty(message = "ENTER REQUIRED FIELDS - LAST NAME")
private String lastName;
@Column(name = "BIRTH_DATE")
@Past(message = "PLEASE ENTER A VALID DATE ")
@NotNull
private Date birthDate;
}
@ControllerAdvice
public static class RestExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleAllExceptionMethod(MethodArgumentNotValidException ex, WebRequest requset) {
// general response dump
return ResponseEntity.badRequest().body(ex.getBindingResult().getAllErrors());
}
}
还要确保不要为birthDate
使用@NotEmpty
,而要使用@NotNull
,因为对于NotEmpty
length of character sequence is evaluated
答案 1 :(得分:0)
这适用于与我有相同问题的任何人。我的问题是 Date 对象来自 java.sql.Date。我将它们更改为 java.util.Date 现在验证有效。