Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Create a Request DTO for Input in Spring Boot
📖 Scenario: You are building a simple Spring Boot application that accepts user data through a web form. To handle this data safely and clearly, you need to create a Request DTO (Data Transfer Object) that will hold the input values.
🎯 Goal: Build a Request DTO class in Spring Boot with fields for name (String) and age (int). This class will be used to receive input data from a client.
📋 What You'll Learn
Create a Java class named UserRequest in the dto package.
Add private fields name (String) and age (int).
Generate public getter and setter methods for both fields.
Add a no-argument constructor and an all-argument constructor.
Use standard Java naming conventions.
💡 Why This Matters
🌍 Real World
Request DTOs are used in web applications to safely receive and validate user input data from forms or API calls.
💼 Career
Understanding how to create and use DTOs is essential for backend developers working with Spring Boot or similar frameworks to build clean and maintainable APIs.
Progress0 / 4 steps
1
Create the UserRequest class with package declaration
Create a Java class named UserRequest inside the dto package. Add the package declaration package dto; at the top.
Spring Boot
Hint
Start by declaring the package and the class skeleton.
2
Add private fields name and age
Inside the UserRequest class, add two private fields: a String name and an int age.
Spring Boot
Hint
Use private String name; and private int age; inside the class.
3
Add constructors: no-argument and all-argument
Add a public no-argument constructor and a public constructor with parameters String name and int age that sets the fields.
Spring Boot
Hint
Write two constructors: one empty and one that sets both fields.
4
Add public getter and setter methods for name and age
Add public getter and setter methods for the name and age fields in the UserRequest class.
Spring Boot
Hint
Write getter and setter methods for both fields following Java naming rules.
Practice
(1/5)
1. What is the main purpose of a Request DTO in a Spring Boot application?
easy
A. To configure the application server settings
B. To store data directly in the database
C. To handle HTTP responses sent to the client
D. To organize and validate user input separately from database models
Solution
Step 1: Understand what a Request DTO is
A Request DTO (Data Transfer Object) is used to receive and organize input data from users in a clean way.
Step 2: Differentiate from other components
Unlike database models, Request DTOs focus only on input validation and structure, not on storing data.
Final Answer:
To organize and validate user input separately from database models -> Option D
Quick Check:
Request DTO = Input organization and validation [OK]
Hint: Request DTOs separate input from database models [OK]
Common Mistakes:
Confusing DTO with database entity
Thinking DTO handles HTTP responses
Assuming DTO configures server
2. Which of the following is the correct way to declare a simple Request DTO class in Spring Boot to receive a user's name and age?
easy
A. public interface UserRequest { String getName(); int getAge(); }
B. public class UserRequest { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
C. @Entity public class UserRequest { private String name; private int age; }
D. @Controller public class UserRequest { private String name; private int age; }
Solution
Step 1: Identify the correct DTO structure
A Request DTO is a simple Java class with private fields and public getters/setters to hold input data.
Step 2: Check annotations and class type
It should NOT be an entity or controller; those are for database and web layers respectively.
Final Answer:
Plain Java class with private fields and getters/setters -> Option B
Quick Check:
DTO = Plain class with getters/setters [OK]
Hint: DTOs are plain classes, not entities or controllers [OK]
Common Mistakes:
Using @Entity annotation on DTO
Making DTO an interface
Annotating DTO as @Controller
3. Given the following Request DTO and controller method, what will be the output if the JSON input is {"name":"Alice","age":30}?
public class UserRequest {
private String name;
private int age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
@PostMapping("/user")
public String createUser(@RequestBody UserRequest request) {
return "User: " + request.getName() + ", Age: " + request.getAge();
}
medium
A. User: null, Age: 0
B. Compilation error due to missing annotations
C. User: Alice, Age: 30
D. Runtime error due to invalid JSON
Solution
Step 1: Understand JSON to DTO mapping
The JSON keys match the DTO fields, so Spring Boot maps "name" to name and "age" to age correctly.
Step 2: Check controller return value
The method returns a string combining name and age from the DTO, so it outputs "User: Alice, Age: 30".
4. Identify the error in this Request DTO class that causes Spring Boot to fail binding JSON input:
public class ProductRequest {
private String productName;
private int quantity;
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
}
medium
A. Missing getter and setter for productName field
B. Quantity field should be public
C. Class must be annotated with @RequestBody
D. No default constructor defined
Solution
Step 1: Check field access and methods
productName lacks getter/setter, so Spring cannot bind JSON to it properly.
Step 2: Validate other options
Quantity has proper getter/setter; @RequestBody is for method parameters, not classes; default constructor is implicit.
Final Answer:
Missing getter and setter for productName field -> Option A
Quick Check:
DTO fields need getters/setters for binding [OK]
Hint: DTO fields need getters/setters for JSON binding [OK]
Common Mistakes:
Thinking public fields bind without getters/setters
Adding @RequestBody on DTO class
Assuming default constructor must be explicit
5. You want to ensure that the email field in your Request DTO is not empty and follows a valid email format. Which annotations should you add to the email field to achieve this validation automatically in Spring Boot?
hard
A. @NotEmpty and @Email
B. @Valid and @NotNull
C. @Size(min=1) and @Pattern(regexp=".+@.+\\..+")
D. @NotBlank and @JsonProperty
Solution
Step 1: Identify validation annotations for non-empty and email format
@NotEmpty ensures the field is not empty, and @Email checks for valid email format.
Step 2: Evaluate other options
@Valid is for nested validation, @NotNull allows empty strings, @Size and @Pattern can work but are more complex; @JsonProperty is for JSON mapping, not validation.
Final Answer:
@NotEmpty and @Email -> Option A
Quick Check:
Use @NotEmpty and @Email for email validation [OK]
Hint: Use @NotEmpty and @Email for email validation [OK]