有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

验证bean在Java11和SpringBoot2.5.3中不起作用

您好在花了几个小时阅读stackoverflow答案并试图找到答案之后,我仍然无法找出为什么像NotNullNotBlank这样的验证器不能在java中工作。我仍然可以发送一个空白名称的post请求。该项目基于Java11和SpringBootStarterV2。5.3. 事先非常感谢如果你能弄明白,我觉得我必须给你买一个冰淇淋:')

在我的模型中:

import javax.validation.constraints.NotBlank;

@NotBlank
private final String name;

在我的控制器中:

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

    @PostMapping(path="add")
    public void addUser(@Valid @NotNull @RequestBody User user) {
        //Request body takes the data taken and converts it into a User
        userService.addUser(user);
    }

    @PutMapping(path="user/{id}")
    public void updateUser(@PathVariable("id") UUID id, @Valid @NotNull @RequestBody User userToUpdate) {
        userService.updateUser(id, userToUpdate);
    }

pom.xml中的我的依赖项中:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </dependency>
    </dependencies>

共 (0) 个答案