首页 > 解决方案 > @NotNull 约束在 Spring Boot 中不起作用

问题描述

我正在使用 Spring Boot 并且 @NotNull 不起作用。当未提供名称值时,该函数运行良好。

@Entity
public class Employee  implements Serializable{
    @Id
    //@GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    //@Id
    public void setName(String name) {
        this.name = name;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }

    @NotNull  // not working
    private String name;
    private String phone;
}

控制器是

@Controller
public class URLController {
    @Autowired
    EmployeeServiceImpl empService;

    @GetMapping({"/", "/index"})
    public String index1(Model model){
        model.addAttribute("employee",new Employee());
        return "index";
    }

    @PostMapping("/result")
    public String result(@Valid @ModelAttribute Employee employee){
        System.out.print(employee.getName()); 
        empService.save(employee);
        return "result"; 
    }
}

pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0

<groupId>com.springs</groupId>
<artifactId>springs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springs</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我正在使用模型属性来获取员工对象。pom.xml 文件包含休眠验证器依赖项。

标签: javaspringspring-mvcspring-boot

解决方案


例如,您必须在代码的映射部分中添加 @Valid 注释

void addEmployee(@Valid Employee emp);

使用此导入“import javax.validation.Valid;”

另一个注意事项,如果你想验证列表,你必须制作一个包装器列表

public class ValidList<E> implements List<E> {

@Valid
private List<E> list;}

并覆盖其中的所有方法


推荐阅读