Home > other >  Can not find any @Not annotations in spring boot v3.01
Can not find any @Not annotations in spring boot v3.01

Time:01-16

I'm trying to implement validation in spring boot but can not find @NotBlank,@NotNull,@NotEmpty , any of the annotations. I specially want to use @NotBlank for my entity fields.

I've tried with the following depenednecies:

implementation 'org.hibernate.validator:hibernate-validator:8.0.0.Final'

implementation 'org.hibernate:hibernate-validator:8.0.0.Final'

implementation 'org.hibernate:hibernate-validator-cdi:8.0.0.Final'

implementation 'org.hibernate.validator:hibernate-validator-annotation-processor:8.0.0.Final'

implementation 'jakarta.annotation:jakarta.annotation-api:2.1.1'

Both

implementation 'org.hibernate:hibernate-validator-cdi:8.0.0.Final'
    implementation 'org.glassfish:jakarta.el:'

implementation 'org.hibernate:hibernate-annotations:3.5.6-Final'

implementation 'org.springframework.boot:spring-boot-starter-validation'

Hibernate website https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-gettingstarted says use both

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>8.0.0.Final</version>
</dependency>

<dependency>
    <groupId>org.glassfish.expressly</groupId>
    <artifactId>expressly</artifactId>
    <version>5.0.0</version>
</dependency>

But I'm not able to find the glassfish.expressly

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.1'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '19'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.mysql:mysql-connector-j'
    annotationProcessor 'org.projectlombok:lombok'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

SOMEBODY HELP WITH ME TO IMPLEMENT THE VALIDATIONS If SpringBoot has removed the @NotBlank,@NotNull,@NotEmpty validations in V3.0.1

CodePudding user response:

If you are using Spring Boot 3.0, just need to add a validation starter. It will add the bean validation APIs and implementations(Hibernate Validator) for you.

In your Gradle build script, add the following

dependencies{
    // ...
    implementation 'org.springframework.boot:spring-boot-starter-validation'
}

Refresh your Gradle build, and all the annotations will be available in the package jakarta.validation.

CodePudding user response:

With Help of @Hantsy and after soo many tries I found the solution. I tried by reloading IDE window. I tried with ./gradlew build --refresh-dependencies but it also didn't not work for me.

Solution

Indeed the following dependency is used for the solution of my question

implementation 'org.springframework.boot:spring-boot-starter-validation'

BUT the reason it wasn't working because the Gradle was not refreshing the dependencies

So to make it work I the rebuild project in my VC CODE

Whenever you add new dependencies always rebuild your project

  • Related