Backend Development 6 min read

Master SpringBoot 3.2 Parameter Validation: @Validated vs @Constraint Explained

This article demonstrates how SpringBoot validates controller request parameters using @Validated, @Valid, and constraint annotations, compares behavior between SpringBoot 2.7.18 and 3.2.5, and shows how to handle validation exceptions with @ControllerAdvice.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master SpringBoot 3.2 Parameter Validation: @Validated vs @Constraint Explained

Environment: SpringBoot 3.2.5.

1. Introduction

Controller interface parameter validation uses JSR‑303 annotations such as @Validated and @Valid . By placing these annotations and constraint annotations like @NotBlank or @Min on method parameters, Spring Boot automatically validates incoming requests and throws an exception or returns errors.

<code>public class User {
  @Min(value = 1, message = "Age must be at least 1")
  private Integer age;
  @NotEmpty(message = "Name cannot be empty")
  private String name;
  // getters, setters
}
@PostMapping("")
public Object user(@RequestBody @Valid User users, BindingResult error) {
  return error.getFieldErrors()
      .stream()
      .map(err -> err.getField() + ": " + err.getDefaultMessage())
      .collect(Collectors.joining(" - "));
}
</code>

The last parameter BindingResult receives validation results; if it is omitted Spring will throw an exception.

2. Interface Parameter Validation

Adding @Constraint annotations on method parameters works, but behavior changed after SpringBoot 3.2.

<code>@RestController
@RequestMapping("/validate")
public class ValidateController {
  @GetMapping("/query")
  public Integer query(@Min(value = 10) Integer id) {
    return id;
  }
}
</code>

Testing on two versions:

SpringBoot 2.7.18

Without class‑level @Validated the request succeeds and no validation error occurs (not the expected result).

Adding @Validated on the controller class triggers a validation exception as expected.

SpringBoot 3.2.5

Without @Validated the request returns a 400 error; the console shows no exception unless the log level is set to DEBUG.

Adding @Validated makes the behavior identical to the 2.7.18 case.

Summary: Starting with SpringBoot 3.2, method‑parameter validation works without needing a class‑level @Validated annotation.

3. Error Handling

After SpringBoot 3.2, validation failures throw HandlerMethodValidationException . It can be handled with @ControllerAdvice to extract a unified error response.

<code>@ExceptionHandler({HandlerMethodValidationException.class})
public ResponseEntity<Map<String, Object>> error(HandlerMethodValidationException exception) {
  Map<String, Object> ret = new HashMap<>();
  ret.put("code", -1);
  exception.visitResults(new HandlerMethodValidationException.Visitor() {
    public void requestParam(RequestParam requestParam, ParameterValidationResult result) {
      String errMsg = result.getResolvableErrors()
          .stream()
          .map(msg -> msg.getDefaultMessage())
          .collect(Collectors.joining(""));
      ret.put("msg", errMsg);
    }
    // other methods omitted for brevity
  });
  return ResponseEntity.status(400).body(ret);
}
</code>

The client receives a JSON response containing the error message.

JavaBackend DevelopmentJSR-303SpringBootParameter Validation
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.