Hibernate验证器抛出异常ValidationException: HV000028,针对@Past注释。

huangapple go评论100阅读模式
英文:

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 &#39;org.springframework.boot:spring-boot-starter-validation&#39;```

The following my a similar entity I have 

```@Entity
@NoArgsConstructor
@Data
public class Person{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
  
    @Column(name = &quot;FIRST_NAME&quot;)
    @NotEmpty(message = &quot;ENTER REQUIRED FIELDS - FIRST NAME&quot;)
    private String firstName;
    @Column(name = &quot;LAST_NAME&quot;)
    @NotEmpty(message = &quot;ENTER REQUIRED FIELDS - LAST NAME&quot;)
    private String lastName; 
    @Column(name = &quot;BIRTH_DATE&quot;)
    @Past(message = &quot;PLEASE ENTER A VALID DATE &quot;)
    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 = &quot;test-person&quot;)
    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 = &quot;FIRST_NAME&quot;)
        @NotEmpty(message = &quot;ENTER REQUIRED FIELDS - FIRST NAME&quot;)
        private String firstName;
        @Column(name = &quot;LAST_NAME&quot;)
        @NotEmpty(message = &quot;ENTER REQUIRED FIELDS - LAST NAME&quot;)
        private String lastName;
        @Column(name = &quot;BIRTH_DATE&quot;)
        @Past(message = &quot;PLEASE ENTER A VALID DATE &quot;)
        @NotNull
        private Date birthDate;

    }

    @ControllerAdvice
    public static class RestExceptionHandler {

        @ExceptionHandler(MethodArgumentNotValidException.class)
        public ResponseEntity&lt;Object&gt; 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.

huangapple
  • 本文由 发表于 2020年10月26日 04:31:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64528479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定