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

2024-04-30

我想验证我的请求正文@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;
    }
}

另一类:

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 be empty")
     @Size(max = 50, message = "Email cannot exceed 50 characters")
     @Pattern(regexp = EMAIL_REGEX_PATTERN, message = "Email should contain a valid email address.")
     private String email;

     // other fields
}

控制器类:

@PostMapping(value = Constants.STUDENT_SIGN_UP)
public Response signUpStudent(@Valid @RequestBody Request request, HttpServletRequest servletRequest) {

    // retrieving the actual resource from request payload
    StudentSignUpRequest signUpRequest = request.getData(StudentSignUpRequest.class);
    // call service to sign-up student
    return loginRegistrationService.signUpStudent(signUpRequest);
}

调用代码设置请求如下:

StudentSignUpRequest studentSignUpRequest = new StudentSignUpRequest();
//setter methods

Request payload = new Request();
payload.setData(studentSignUpRequest);

这是我发送的请求:

对于超过 64 个字符的名字:

示例 JSON:

{
    "data": {
        "firstName": "student111111111111111111111111111111111111111111111111111111111111",
        "lastName": "somesurname",
        "email": "[email protected] /cdn-cgi/l/email-protection"
    }
}

如果不包括名字:

{
    "data": {
        "lastName": "somesurname",
        "email": "[email protected] /cdn-cgi/l/email-protection"
    }
}

这里两个@Size@NotNull注释不起作用。

有什么解决办法吗?


验证会起作用,如果Request课堂就像;

public class Request {

    @Valid
    StudentSignUpRequest data;

    // other stuff
}

事实上您没有班级类型data使得验证无法应用于它,忽略了甚至不存在的事实@Valid https://docs.oracle.com/javaee/7/api/javax/validation/Valid.html字段上的注释。这@Valid注释用于传播验证级联。

但由于你无法修改Request对象,让我们继续使用另一种方法来处理验证,而无需手动执行。


另一种方法是在获得后触发验证StudentSignUpRequest from request object;

StudentSignUpRequest signUpRequest = request.getData(StudentSignUpRequest.class);
loginRegistrationService.signUpStudent(signUpRequest) // validation will trigger with this call

您可以执行以下操作;

@Service
@Validated
public class LoginRegistrationService {

    public void signUpStudent(@Valid StudentSignUpRequest signUpRequest) {
        // some logic
    }
}

with @Validated https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/annotation/Validated.html注释,您将激活任何的验证检查@Valid带注释的参数public该类中的方法。

可以与方法级别验证一起使用,表明 特定的类应该在方法级别进行验证(作用 作为相应验证拦截器的切入点)

这可能代价高昂,因为您希望尽快获得任何约束违规,而不需要为已经注定的请求执行任何昂贵的工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在 Spring boot 中使用 @Valid 验证请求正文 的相关文章

随机推荐