5

All.
I want to use @Valid and @NotEmpty in the spring boot(2.3.0) framework.
But I was unable to import javax.validation so I couldn't.
Here is the screen and build.gradle file I am currently experiencing:

  1. Can't be used @Valid annotationenter image description here
  2. Can't be used @NotEmpty annotationenter image description here

<File: build.gradle>

plugins {
    id 'org.springframework.boot' version '2.3.0.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'kr.co.fastcampus'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    //  Spring Boot
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-validation'

    //  Database
    implementation 'com.h2database:h2'

    compileOnly 'org.projectlombok:lombok'

    developmentOnly 'org.springframework.boot:spring-boot-devtools'

    annotationProcessor 'org.projectlombok:lombok'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}
Kainix
  • 1,186
  • 3
  • 21
  • 33
Changjoo Sohn
  • 53
  • 1
  • 4
  • May be the gradle is not synced correctly. Try rebuilding your project , Right click on the project -> Build Module . – Tanuj Jul 06 '20 at 03:13

3 Answers3

8

I came here with the same issue, but my problem was that I forgot to add this line to build.gradle.

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

I know this was not your issue, but just posting this here for others who face the same issue like me and stumble on to this question.

VishnuVS
  • 1,055
  • 2
  • 14
  • 29
2

Might be problem with gradle cache.

Make sure you are using correct gradle version in gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip

Then do a rebuild. Check if you are getting any errors during build. Everything should be green here:

build output

I used the same build.gradle file (which you provided), both @Valid and @NonEmpty annotations are working fine.

Abhinaba Chakraborty
  • 3,488
  • 2
  • 16
  • 37
  • Hi, Abhinaba. Thank you for answering as soon as posible. Here is gradle-wrapper.properies: I think distributionUrl is same with yours. `distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists` – Changjoo Sohn Jul 06 '20 at 04:28
  • Ok cool. Please try rebuilding and see if it is all 'green' for you. – Abhinaba Chakraborty Jul 06 '20 at 04:29
  • Would you mind to explain with VSCode? Your capture image looks like IntelliJ environment which you share to me. However, I need to use the VSCode to make project. And I'm not good at making java project with VSCode. – Changjoo Sohn Jul 06 '20 at 07:31
  • @ChangjooSohn Please check this out and try running the build: https://www.syncfusion.com/ebooks/gradle_succinctly/running-gradle-from-visual-studio-code#:~:text=To%20run%20Gradle%20from%20VS%20Code%2C%20the%20project's%20folder%20must,%2B%20Shift%20%2B%20P%20keys%20combination. – Abhinaba Chakraborty Jul 06 '20 at 07:48
0

To use bean validation (Javax.validation), in most cases you also need an implementation. You can think of the javax.validation package like an interface, it just contains the validation apis, but they must be implemented through another package.

This used to be hibernate.validator, but it is now deprecated.

Speaking of deprecated, the entire javax libraries module is deprecated in the newest version. This includes the json libraries, the jax-rs and jax-ws libs, and a lot of others.

With that said, and noting that he uses the old hibernate package that no longer works(been there done that, you do not know the headache that I just saved you), then here is a great tutorial on the subject. He uses maven as do most spring boot projects, but you can tweak it for gradle pretty simply.

For an implementation package, google "bean validator implementation 2020". That should handle it.

Nate T
  • 727
  • 5
  • 21
  • 1
    @Changjoo Sohn You are currently using spring boot starter validator, which uses the hibernate package under the hood. I am warning you, you are in for a huge headache!! – Nate T Dec 21 '20 at 12:45