0
0
Spring Bootframework~20 mins

Request DTO for input in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Request DTO Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a Spring Boot controller receives this Request DTO?

Consider this Spring Boot Request DTO and controller method:

public record UserRequest(String name, int age) {}
@PostMapping("/user")
public ResponseEntity createUser(@RequestBody UserRequest request) {
    return ResponseEntity.ok("User " + request.name() + " is " + request.age() + " years old.");
}

If the JSON {"name":"Alice","age":30} is sent, what will the response body be?

AHTTP 400 Bad Request error
B"User null is 0 years old."
C"User Alice is 30 years old."
D"User Alice is null years old."
Attempts:
2 left
💡 Hint

Records automatically map JSON fields to constructor parameters by name.

📝 Syntax
intermediate
2:00remaining
Which Request DTO declaration is syntactically correct in Spring Boot using Java records?

Choose the correct Java record declaration for a Request DTO with fields email (String) and subscribed (boolean):

Apublic record SubscriptionRequest(String email, Boolean subscribed) {}
Bpublic record SubscriptionRequest(String email; boolean subscribed) {}
Cpublic record SubscriptionRequest { String email; boolean subscribed; }
Dpublic record SubscriptionRequest(String email, boolean subscribed) {}
Attempts:
2 left
💡 Hint

Records use parentheses with comma-separated components.

🔧 Debug
advanced
2:00remaining
Why does this Spring Boot controller fail to bind the JSON to the Request DTO?

Given this Request DTO and controller method:

public record ProductRequest(String productName, int quantity) {}
@PostMapping("/product")
public ResponseEntity addProduct(ProductRequest request) {
    return ResponseEntity.ok("Added " + request.productName() + ", qty: " + request.quantity());
}

When sending JSON {"productName":"Pen","quantity":10}, the server returns HTTP 400. Why?

AMissing @RequestBody annotation on the method parameter causes binding failure.
BRecord fields must be public for binding to work.
CJSON keys do not match record component names.
DThe controller method must return void for POST requests.
Attempts:
2 left
💡 Hint

Spring Boot needs an annotation to know where to get the data from.

state_output
advanced
2:00remaining
What is the value of the 'age' field after deserialization?

Given this Request DTO record and JSON input:

public record PersonRequest(String name, Integer age) {}

JSON sent: {"name":"Bob"}

What will be the value of age() in the deserialized object?

AUndefined, depends on JVM
Bnull
C0
DThrows NullPointerException
Attempts:
2 left
💡 Hint

Consider the difference between primitive and wrapper types.

🧠 Conceptual
expert
2:00remaining
Which statement about using Java records as Request DTOs in Spring Boot is true?

Choose the correct statement about Java records used as Request DTOs in Spring Boot:

ARecords automatically provide immutable data carriers suitable for Request DTOs.
BRecords require a no-argument constructor to be used as Request DTOs.
CRecords cannot be used as Request DTOs because they lack setters.
DRecords require explicit Jackson annotations on every field to deserialize JSON.
Attempts:
2 left
💡 Hint

Think about how records handle data and immutability.