Home > Net >  How to import @NotBlank after adding org.springframework.boot dependency in pom.xml
How to import @NotBlank after adding org.springframework.boot dependency in pom.xml

Time:01-29

I am new to spring-boot and I am following freecodecamp's tutorial and there the tutor imported @NotBlank, I am trying to import it from org.springframework.boot but it is not working. Can anyone tell me how to import @NotBlank.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>learning-spring</name>
    <description>Trying to learn</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>20.1.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

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

</project>

This is pom.xml and I have added org.springframework.boot in the dependency

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

I don't know how to import it into my code?

This is my code, help me out.

package com.example.demo.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.UUID;
public class Person
{
    private final UUID id;

    @NotBlank
    private final String name;

    public Person(@JsonProperty("id")UUID id,
                  @JsonProperty("name") String name)
    {
        this.id = id;
        this.name = name;
    }
    public UUID getId()
    {
        return id;
    }
    public String getName()
    {
        return name;
    }
}

Thanks in advance.

I don't know how to import it, earlier I try tried to import javax.validation but it didn't work.

CodePudding user response:

The following import should work:

import javax.validation.constraints.NotBlank;

CodePudding user response:

From Spring Boot 3.0.0 it uses Java 17. In Java 17 the location of of validators has moved from javax.validation to jakarta.validation - see Bug: Spring Boot 3.0.0-M1 cannot find javax.validation#2953 - not really a bug.

Your options are to stay with spring-boot-starter-parent v3.0.2 (jdk17) and use the following import:

import jakarta.validation.constraints.NotBlank;

Or to drop spring-boot-starter-parent to a version before 3.0.0, eg 2.7.8 and use the following import. I recomment this unless you really need Java17.

import javax.validation.constraints.NotBlank;
  • Related