0
0
Spring Bootframework~10 mins

@Valid annotation on request body in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AValid
BAutowired
CRequestParam
DPathVariable
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
2fill in blank
medium

Complete 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'
Avalidation.executable
Bvalidation
Cvalidation.metadata
Dvalidation.constraints
Attempts:
3 left
💡 Hint
Common Mistakes
Importing from javax.validation.constraints which is for specific constraint annotations
Importing from unrelated sub-packages
3fill in blank
hard

Fix 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'
A@Validated
B@NotNull
C@RequestParam
D@Valid
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
4fill in blank
hard

Fill 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'
A@Valid
B>
C<=
D@Autowired
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @Valid
Using <= instead of > for price check
5fill in blank
hard

Fill 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'
AValid
BNotNull
CEmail
DAutowired
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @Valid
Swapping @NotNull and @Email
Forgetting constraint annotations on the model