I'm building a Spring Boot 3.4.6 application using MongoDB and encountering an issue where @NonNull and @Email annotations are not triggering validation errors/exceptions even though I am using @Valid @RequestBody in the Controller. The only validation that is working is @Indexed (unique = true)
(Yess, @NonNull and @Email are 2 completely different annotations...)
I have tried:
- Rebuilding my entire project (
mvn clean build) - Checking for the required dependencies
- Confirmed all annotations are from
jakarta.validation.*, notjavax.validation.*(for@Emailannotation) - Sending clear invalid inputs using Postman
Yet, no validation error is triggered, and the user still gets created.
Here’s my setup:
User.java Entity Class
package com.sathwikhbhat.journalApp.entity;
import jakarta.validation.constraints.Email;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "users")
public class User {
@Id
private ObjectId id;
@Indexed(unique = true)
@NonNull
private String userName;
@NonNull
@Indexed(unique = true)
@Email
private String email;
private boolean sentimentAnalysis;
@NonNull
private String password;
@DBRef
private List<JournalEntry> journalEntries = new ArrayList<>();
private List<String> roles;
}
PublicController.java Controller Class
package com.sathwikhbhat.journalApp.controller;
import com.sathwikhbhat.journalApp.entity.User;
import com.sathwikhbhat.journalApp.service.UserService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/public")
public class PublicController {
@Autowired
private UserService userService;
@PostMapping("createUser")
public ResponseEntity<?> createUser(@Valid @RequestBody User user) {
try {
userService.saveNewUser(user);
log.info("User created successfully: {}", user.getUserName());
return new ResponseEntity<>(HttpStatus.CREATED);
} catch (DuplicateKeyException e) {
log.error(e.getMessage());
return new ResponseEntity<>("Username already exists: ", HttpStatus.CONFLICT);
} catch (Exception e) {
log.error(e.getMessage());
return new ResponseEntity<>("Internal server error: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
Any insights or suggestions would be greatly appreciated!
@NonNullis from Lombok and doesn't do anything for validation only object construction. Wild guess you thought it would be a good idea to just addvalidation-apias a dependency and expected all to work. Instead addspring-boot-starter-validationand ditch thevalidation-apidependency.spring-boot-starter-validationas that will pull in the correct and compatible dependencies. If you use the wrong mix of versions between API and the hibernate-validator it won't work.