Home > OS >  Spring Boot Validation is not working with javax.validation
Spring Boot Validation is not working with javax.validation

Time:12-28

I am working on a Spring Boot project and I am currently trying to implement validation. For example, I have the following class:

package abcdef.mypackage

import java.util.*
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.validation.constraints.Email
import javax.validation.constraints.NotBlank

@Entity
class User (
    @Id
    @GeneratedValue
    var id: Long,

    @Column(name="username", unique = true, nullable = false)
    @NotBlank
    var username: String,

    @Column(name="password", unique = false, nullable = false)
    var password: String,

    @Column(name="firstname", unique = false, nullable = false)
    @NotBlank
    var firstname: String,

    @Column(name="lastname", unique = false, nullable = false)
    @NotBlank
    var lastname: String,

    @Column(name = "birthdate", unique = false, nullable = true)
    var birthdate: Date? = null,

    @Column(name="email", unique = true, nullable = false)
    @Email
    var email: String,

    @Column(name="phone", unique = true, nullable = false)
    var phone: String,
)

You can see, that I have annotated all fields with the validations I want to have. Incoming requests are handled by the following controller class:

package abcdef.mypackage

import org.springframework.http.ResponseEntity
import org.springframework.validation.BindingResult
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import org.springframework.web.server.ResponseStatusException
import javax.validation.Valid

@RestController
@RequestMapping("/api/v1/users/")
@Validated
class UserResource(val service: UserService) {

    @PostMapping("/register")
    @Validated
    fun post(@Valid @RequestBody user: User, result: BindingResult) : ResponseEntity<Unit> {

        if (result.hasErrors()) {
            return ResponseEntity.badRequest().build()
        }

        try {
            service.post(user)
        } catch (e: Exception) {
            return ResponseEntity.badRequest().build()
        }

        return ResponseEntity.ok().build()
    }

}

When I now make a request with for example a blank username value, Spring Boot still accepts it and stores into the database. I have found some questions (and answers) on StackOverflow about missing dependencies, but I included all of those. You can take a look at my dependecies here:

<dependencies>

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </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-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
    </dependencies>

I have no Idea what to do to get this validation working... I cannot see any issue in my dependencies. I also tried some variations with the usage of @Valid and @Validated but nothing worked for me.

I hope somebody sees my mistake. Thanks a lot!

CodePudding user response:

Example: recommend data class @field:NotBlank and not need @Validated

  • Related