SpringBoot + Java 17 @Valid not validating POJO

Advertisements

I was recently trying to learn Spring and Java 17 as I only have experience with Dropwizard and Java 11, and I would like to do validation on a POJO, in a POST or PUT request.
I’ve encountered several examples, but locally testing, I don’t seem to be able to actually run into the validation, at first I thought it might be a Records issue so I’ve fallen back to Class and I still cannot validate it, any clues as to what the issue might be?

My Controller:

@RestController
public class HelloWorldController {

//... other GET APIs

  @PostMapping("/userProfile")
  @ResponseStatus(HttpStatus.CREATED)
  public ResponseEntity<UserProfileClass> addUserProfile(@RequestBody @Valid UserProfileClass userProfile) {
    System.out.println("Inserting in the database: " + userProfile);
    return ResponseEntity.ok(userProfile);
  }

POJO (UserProfileClass):

public class UserProfileClass {

    @NotNull @NotBlank String name;
    @NotBlank @NotNull String address;
    //... getters and setters

pom.xml containing validators:

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

If I run it locally and I do a POST on IntelliJ with the following:

POST http://localhost:8080/userProfile
Content-Type: application/json

{
  "name": "some Name"
}

I always get a 200 with the following body:

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 18 Feb 2024 23:21:46 GMT
Keep-Alive: timeout=60
Connection: keep-alive

{
  "name": "some Name",
  "address": null
}

As stated obviously the @NotNull shouldn’t let this go through, I’m assuming that if the @Valid fails I would get some sort of 4XX response, but that doesn’t seem to be happening.
What’s missing here?

>Solution :

The problem is that you’re likely relying on the wrong version of the bean validation API. You’re including v2 of this API, but with Spring Boot 3, you should be relying on Jakarta EE 9, which requires v3 of the bean validation API.

To solve this, you need to remove the validation-api dependency. You don’t need to add anything in stead because spring-boot-starter-validation already includes the right validation API dependency (through hibernate-validator).

After removing the dependency, you’ll have to change your imports from javax.validation.* to jakarta.validation.*.

Leave a ReplyCancel reply