简体   繁体   中英

import javax.validation.constraints.notblank not working

I am trying to use @NotBlank in my code and it required the dependency and import of javax.validation.constraints.NotBlank . However, the import just does not work after I put in the dependency, any guidance would be appreciated.

pom.xml

        <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>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </dependency>

Java Class

package com.educative.ecommerce.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.validation.constraints.NotBlank;

@Entity
@Table(name = "categories")
public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "category_name")
    private @NotBlank String categoryName;

    private @NotBlank String description;

    private @NotBlank String imageUrl;
}

The import javax.validation.constraints.NotBlank; is showing error of Cannot resolve symbol 'validation'

The @NotBlank from package javax.validation.* is only available from version 2.0 of the following dependency.

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.0.Final</version>
</dependency>

If you have an older dependency then this should not be available under the above package but you can still find this annotation inside org.hibernate.validator.constraints with the following import:

import org.hibernate.validator.constraints.NotBlank;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM