我在网上关注了多个帖子,比如https://www.javatpoint.com/restful-web-services-validationshttps://reflectoring.io/bean-validation-with-spring-boot/等等。
到处都写着,只要在@RequestBody前面添加@Valid就可以检查输入的有效性。但对我来说,它总是返回状态码为200,尽管使用JUnit测试用例,我能够验证所有的限制。
我也关注了像Spring Boot Validations with annotations这样的帖子,并添加了BindingResult作为第二个参数,仍然是相同的行为,bindingResult对象显示0错误。
详情如下:
DTO类:
package practice.corejava.validators.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserData {
@NotBlank(message = "Name should not be blank")
private String name;
@NotNull(message = "Age should not be null")
@Positive(message = "Age should be positive")
private Integer age;
}
控制器类别:(总是通过 Postman 返回201)
package practice.corejava.validators.controller;
import practice.corejava.validators.dto.UserData;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import jakarta.validation.Valid;
import java.net.URI;
@RestController
@RequestMapping("api/v1/validations")
public class ValidationsController {
@PostMapping(value = "/user-data", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UserData> createUserData(@Valid @RequestBody UserData userData) {
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{name}").buildAndExpand(userData.getName()).toUri();
return ResponseEntity.created(location).build();
}
}
JUnit测试类:(测试用例正在成功)
package practice.corejava.validators.dto;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class UserDataTest {
private static Validator validator;
@BeforeAll
public static void setUp() {
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
validator = factory.getValidator();
}
}
@Test
void all_validation_success() {
UserData userData = new UserData("SomeName", 30);
Set<ConstraintViolation<UserData>> constraintViolations = validator.validate(userData);
assertEquals(0, constraintViolations.size());
}
@Test
void name_blank() {
UserData userData = new UserData("", 30);
Set<ConstraintViolation<UserData>> constraintViolations = validator.validate(userData);
assertEquals(1, constraintViolations.size());
assertEquals("Name should not be blank", constraintViolations.iterator().next().getMessage());
}
}
Postman 请求正文:预期为400,但201作为状态代码出现
{
"name" : null,
"age" : 30
}
请参阅下面的“权限和版本”详细信息:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.3</version>
<relativePath/>
</parent>
<properties>
<java.version>17</java.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>
1条答案
按热度按时间mwecs4sa1#
正如M.代努姆在评论中,我使用的依赖与Sping Boot 3.x不兼容。因此,React并没有像预期的那样。在更新了建议的更改后,现在它完美地工作了。非常感谢你的建议和时间@M。代努姆