You're tasked with validating user input across multiple forms in a Spring Boot web application. Describe your approach to maintain consistency in validation rules.

Consistent form validation across a Spring Boot application is best achieved by centralizing rules in shared model classes and reusable validators rather than duplicating checks in each controller.

Key Points

Example

Instead of writing separate null and length checks in each of five different form controllers, the same AddressDto class carrying @NotBlank and @Pattern annotations is reused everywhere addresses are submitted, guaranteeing identical rules.

// One DTO with validation rules, reused by every form that captures an address
public record AddressDto(
        @NotBlank String line1,
        @NotBlank @Size(max = 60) String city,
        @Pattern(regexp = "\\d{6}", message = "PIN code must be 6 digits") String pinCode) {}

public record SignupRequest(
        @NotBlank @Email String email,
        @Size(min = 8) String password,
        @Valid @NotNull AddressDto address) {}            // nested validation

@PostMapping("/signup")
public ResponseEntity<Void> signup(@Valid @RequestBody SignupRequest request) {
    userService.register(request);
    return ResponseEntity.status(HttpStatus.CREATED).build();
}

// One place that turns validation errors into a clean 400 response
@RestControllerAdvice
class ValidationErrors {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    ProblemDetail handle(MethodArgumentNotValidException ex) {
        ProblemDetail pd = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
        ex.getBindingResult().getFieldErrors()
          .forEach(e -> pd.setProperty(e.getField(), e.getDefaultMessage()));
        return pd;
    }
}

Interview Tip

A concise interview answer is

I keep validation consistent by putting standard constraint annotations on shared model classes instead of duplicating checks per controller, writing a custom ConstraintValidator for any cross-field logic, and centralizing error formatting in a @ControllerAdvice so every form gets the same rules and the same error response shape.

Last updated: 1 November 2026

Previous question: Can you use third-party libraries for form validation in Spring Boot? How?

Next question: What are the ways to deploy a Spring Boot application?

More Spring Boot - Level IV (Advanced) interview questions

All Spring Boot - Level IV (Advanced) questions