A Request DTO (Data Transfer Object) helps organize and carry data sent by users to your Spring Boot app in a simple, clear way.
Request DTO for input in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
public class UserRequestDTO { private String name; private int age; // Getters and setters 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; } }
DTO classes usually have private fields with public getters and setters.
Spring Boot automatically converts JSON input to this DTO when used with @RequestBody.
public class LoginRequestDTO { private String username; private String password; // getters and setters }
public record ProductRequestDTO(String name, double price) {}public class RegisterRequestDTO { @NotBlank private String email; @Size(min = 6) private String password; // getters and setters }
This example shows a simple DTO class UserRequestDTO and a Spring Boot controller that accepts JSON input mapped to this DTO. When you send a POST request with JSON like {"name":"Alice","age":30}, the controller returns a confirmation message.
package com.example.demo.dto; public class UserRequestDTO { 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; } } package com.example.demo.controller; import com.example.demo.dto.UserRequestDTO; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @PostMapping("/user") public String createUser(@RequestBody UserRequestDTO userRequest) { return "User " + userRequest.getName() + " aged " + userRequest.getAge() + " created."; } }
Always keep DTOs simple and focused only on data transfer.
Use validation annotations to catch bad input early.
Spring Boot automatically maps JSON fields to DTO fields by name.
Request DTOs organize user input into simple classes.
They help keep your controller code clean and easy to read.
Spring Boot maps JSON input to DTOs automatically with @RequestBody.
Practice
Request DTO in a Spring Boot application?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 DQuick Check:
Request DTO = Input organization and validation [OK]
- Confusing DTO with database entity
- Thinking DTO handles HTTP responses
- Assuming DTO configures server
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 BQuick Check:
DTO = Plain class with getters/setters [OK]
- Using @Entity annotation on DTO
- Making DTO an interface
- Annotating DTO as @Controller
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();
}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".Final Answer:
User: Alice, Age: 30 -> Option CQuick Check:
Matching JSON fields produce correct output [OK]
- Assuming missing annotations cause errors
- Expecting null values despite matching JSON
- Confusing runtime errors with mapping
public class ProductRequest {
private String productName;
private int quantity;
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
}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 AQuick Check:
DTO fields need getters/setters for binding [OK]
- Thinking public fields bind without getters/setters
- Adding @RequestBody on DTO class
- Assuming default constructor must be explicit
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?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 AQuick Check:
Use @NotEmpty and @Email for email validation [OK]
- Using @NotNull which allows empty strings
- Confusing @Valid with validation annotations
- Using @JsonProperty for validation
