Performance: @Valid annotation on request body
This affects server-side request validation timing and can indirectly impact user experience by delaying response time if validation is slow.
Jump into concepts and practice - no test required
public ResponseEntity<String> createUser(@Valid @RequestBody User user) {
return ResponseEntity.ok("User created");
}
// User class with validation annotations
public class User {
@NotBlank
private String name;
// getters and setters
}public ResponseEntity<String> createUser(@RequestBody User user) {
// manual validation logic here
if(user.getName() == null || user.getName().isEmpty()) {
return ResponseEntity.badRequest().body("Name is required");
}
// more manual checks
return ResponseEntity.ok("User created");
}| Pattern | CPU Usage | Latency Impact | Error Handling Speed | Verdict |
|---|---|---|---|---|
| Manual validation in controller | High due to repeated checks | Increases latency by milliseconds | Slower error response | [X] Bad |
| @Valid annotation with standard constraints | Low to moderate, optimized by framework | Minimal latency added | Fast error response | [OK] Good |
@Valid annotation on a @RequestBody parameter in a Spring Boot controller?@Valid@Valid annotation triggers validation of the request body object based on constraints defined in its class.@RequestBody, it ensures the data is checked before the controller method uses it.@Valid validates input data [OK]@Valid in a Spring Boot controller method parameter?@Valid must come before @RequestBody on the same parameter.@Valid before @RequestBody on the parameter.{}?
public record User(@NotBlank String name, @Min(18) int age) {}
@PostMapping("/users")
public ResponseEntity<String> addUser(@Valid @RequestBody User user) {
return ResponseEntity.ok("User added");
}@NotBlank requires 'name' to be non-empty; @Min(18) requires 'age' to be at least 18.@PostMapping("/register")
public ResponseEntity<String> registerUser(@RequestBody @Valid User user) {
return ResponseEntity.ok("Registered");
}
But when you send invalid data, no validation errors occur and the method runs normally. What is the most likely cause?@NotNull, @NotBlank, etc., @Valid has nothing to check.name (required, not blank) and price (required, positive number). Which of the following code snippets correctly applies @Valid and validation annotations to ensure invalid data is rejected automatically?@NotBlank ensures 'name' is not empty; @Positive ensures 'price' is greater than zero.@Valid before @RequestBody in the controller method parameter.@Valid. public record Product(String name, double price) {}
@PostMapping("/products")
public ResponseEntity<String> addProduct(@RequestBody @Valid Product product) {
return ResponseEntity.ok("Product added");
} misses validation annotations. public record Product(@NotNull String name, @Min(1) double price) {}
@PostMapping("/products")
public ResponseEntity<String> addProduct(@RequestBody Product product) {
return ResponseEntity.ok("Product added");
} uses @Min(1) on a double (should use @Positive) and misses @Valid.