Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to enable validation on the request body parameter.
Spring Boot
public ResponseEntity<String> createUser(@[1] @RequestBody User user) { return ResponseEntity.ok("User created"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @Valid
Forgetting to add @Valid before @RequestBody
Using @RequestParam or @PathVariable which are for different purposes
✗ Incorrect
The @Valid annotation tells Spring Boot to validate the User object based on its validation annotations.
2fill in blank
mediumComplete the code to import the correct package for the @Valid annotation.
Spring Boot
import javax.[1].Valid;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing from javax.validation.constraints which is for specific constraint annotations
Importing from unrelated sub-packages
✗ Incorrect
The @Valid annotation is in the javax.validation package.
3fill in blank
hardFix the error in the controller method to properly validate the request body.
Spring Boot
public ResponseEntity<String> updateUser(@RequestBody User user) { if(user.getName() == null) { return ResponseEntity.badRequest().body("Name is required"); } return ResponseEntity.ok("User updated"); } // Add [1] to enable automatic validation Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Validated instead of @Valid on method parameters
Not adding any validation annotation
Using @NotNull which is a constraint, not a validation trigger
✗ Incorrect
Adding @Valid before @RequestBody triggers automatic validation of the User object.
4fill in blank
hardFill both blanks to create a controller method that validates the request body and returns a response.
Spring Boot
public ResponseEntity<String> addProduct([1] @RequestBody Product product) { if(product.getPrice() [2] 0) { return ResponseEntity.badRequest().body("Price must be positive"); } return ResponseEntity.ok("Product added"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @Valid
Using <= instead of > for price check
✗ Incorrect
Use @Valid to validate the product object and > to check if price is positive.
5fill in blank
hardFill all three blanks to create a method that validates a request body and uses a constraint annotation on the model.
Spring Boot
public ResponseEntity<String> registerUser(@[1] @RequestBody User user) { return ResponseEntity.ok("User registered"); } // In User class: private String email; @[2](message="Email is required") @[3] private String email;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @Valid
Swapping @NotNull and @Email
Forgetting constraint annotations on the model
✗ Incorrect
Use @Valid on the method parameter, @NotNull to ensure email is not null, and @Email to check email format.