小编pan*_*ish的帖子

无法使用@Valid在Spring Boot中验证请求正文

我想用@Valid批注验证我的请求正文,但在Spring Boot中不起作用

我在JAR文件中有一个Request类,无法使用两个字段进行修改。一个字段是对象类型。我的控制器类接受该类对象作为请求主体。当我将下面的JSON传递给控制器​​时,验证不起作用。下面是代码示例。

要求类别:

public class Request {

    Object data;
    Map<String, Object> meta;

    public <T> T getData() throws ClassCastException {
        return (T) this.data;
    }
}
Run Code Online (Sandbox Code Playgroud)

另一类:

public class StudentSignUpRequest {

     @NotNull(message = "First Name should not be empty")
     @Size(max = 64, message = "FirstName should not exceed 64 characters")
     private String firstName;

     @NotNull(message = "Last Name should not be empty")
     @Size(max = 64, message = "LastName should not exceed 64 characters")
     private String lastName;

     @NotNull(message = "Email cannot …
Run Code Online (Sandbox Code Playgroud)

java spring jackson bean-validation spring-boot

3
推荐指数
1
解决办法
1010
查看次数

标签 统计

bean-validation ×1

jackson ×1

java ×1

spring ×1

spring-boot ×1