【问题标题】:Spring @valid annotation cannot be resolvedSpring @valid 注解无法解析
【发布时间】:2021-03-11 07:51:07
【问题描述】:

我正在尝试创建一个简单的 JPA 框架来执行简单的 CRUD 操作,例如保存员工、删除员工或获取简单的员工。

当我使用有效注释时

    @PostMapping("/employees")
    public Employee createEmployee(@Valid @RequestBody Employee emp)
        return empdao.save(emp);

它的意思是Valid不能被解析为一个类型

我正在使用下面列出的这些启动器依赖项,我不知道某处是否存在冲突

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>       
  </dependencies>

请提供解决方案,或者我应该一起跳过有效注释,如果没有有效注释,它是否可以工作。

【问题讨论】:

标签: java hibernate validation spring-data-jpa


【解决方案1】:

你需要验证依赖,这应该可以解决你的问题:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2015-01-04
    相关资源
    最近更新 更多