Bird
0
0

Given this controller method:

medium📝 Debug Q6 of 15
Spring Boot - Request and Response Handling
Given this controller method:
@PostMapping("/items")
public ResponseEntity addItem(@RequestBody ItemDto item) {
    return ResponseEntity.ok("Item added");
}
And this DTO:
public class ItemDto {
  @NotBlank
  private String title;
}
If the client sends an empty string for title, what is the likely error in the code that prevents validation from working?
AMissing <code>@Validated</code> on the controller class.
BMissing <code>@Valid</code> on the method parameter.
CUsing <code>@NotBlank</code> instead of <code>@NotNull</code>.
DReturning ResponseEntity instead of void.
Step-by-Step Solution
Solution:
  1. Step 1: Check validation trigger on method parameter

    Validation on request body requires @Valid on the parameter to trigger validation.
  2. Step 2: Identify missing or incorrect annotation

    If @Valid is missing, validation annotations like @NotBlank are ignored.
  3. Final Answer:

    Missing @Valid on the method parameter. -> Option B
  4. Quick Check:

    @Valid missing disables validation = D [OK]
Quick Trick: Always put @Valid on request body parameters [OK]
Common Mistakes:
  • Assuming @Validated on controller triggers request body validation
  • Confusing @NotBlank with validation trigger
  • Thinking return type affects validation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes