英文:
hibernate validator throws exception ValidationException: HV000028 for @Past annotation
问题
以下是翻译好的内容:
我正在一个Spring Boot项目中使用Hibernate Validator来验证实体。我在使用@Past日期验证器时遇到了异常。异常信息如下:javax.validation.ValidationException: 在调用isValid时发生意外异常 HV000028
。
以下是我在gradle.build文件中使用的依赖项:
implementation 'org.springframework.boot:spring-boot-starter-validation'
以下是我一个类似的实体示例:
@Entity
@NoArgsConstructor
@Data
public class Person{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "FIRST_NAME")
@NotEmpty(message = "请输入必填字段 - 名")
private String firstName;
@Column(name = "LAST_NAME")
@NotEmpty(message = "请输入必填字段 - 姓")
private String lastName;
@Column(name = "BIRTH_DATE")
@Past(message = "请输入有效日期")
private Date birthDate;
}
以下是日志跟踪信息:
2020-10-25 13:09:11.421 DEBUG 21952 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : 已完成 500 内部服务器错误```
<details>
<summary>英文:</summary>
I am validating an entity with a hibernate validator in a spring boot project. I got an exception for the Date validator @Past . I faced ```javax.validation.ValidationException: HV000028: Unexpected exception during isValid call```
This is the dependency I have in my gradle.build file
```implementation 'org.springframework.boot:spring-boot-starter-validation'```
The following my a similar entity I have
```@Entity
@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;
}
This is a log trace
2020-10-25 13:09:11.421 DEBUG 21952 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Completed 500 INTERNAL_SERVER_ERROR```
</details>
# 答案1
**得分**: 0
我迅速在我的示例项目上运行了一下,所有的配置都已经准备好了。我认为日期格式可能是个问题。可用的默认格式有:
- yyyy-MM-dd'T'HH:mm:ss.SSSX
- yyyy-MM-dd'T'HH:mm:ss.SSS
- EEE,dd MMM yyyy HH:mm:ss zzz
- yyyy-MM-dd
以下是我是如何做的:
控制器
```java
@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 = "请输入必填字段 - 名字")
private String firstName;
@Column(name = "LAST_NAME")
@NotEmpty(message = "请输入必填字段 - 姓氏")
private String lastName;
@Column(name = "BIRTH_DATE")
@Past(message = "请输入有效日期")
@NotNull
private Date birthDate;
}
@ControllerAdvice
public static class RestExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleAllExceptionMethod(MethodArgumentNotValidException ex, WebRequest request) {
// 通用响应输出
return ResponseEntity.badRequest().body(ex.getBindingResult().getAllErrors());
}
}
同时请确保对于 birthDate
字段,不要使用 @NotEmpty
,而是使用 @NotNull
,因为对于 NotEmpty
,会评估字符序列的长度。
英文:
I Quickly ran on my sample project where everything was already configured i think that Date format is the issue. Available default formats are
- yyyy-MM-dd'T'HH:mm:ss.SSSX
- yyyy-MM-dd'T'HH:mm:ss.SSS
- EEE, dd MMM yyyy HH:mm:ss zzz
- yyyy-MM-dd
Here is how i have done it
Controller
@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());
}
}
Also make sure that for birthDate
you should not use @NotEmpty
instead use @NotNull
because for NotEmpty
length of character sequence is evaluated
答案2
得分: 0
这是给所有遇到和我一样问题的人的解决方法。我的问题是日期对象来自于java.sql.Date。我将它们改为java.util.Date,现在验证可以正常工作。
英文:
This is for anyone having the same issue as mine. My issue was the Date objects were from java.sql.Date. I changed them to java.util.Date now the validation works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论