In a Spring Boot application, why do developers use a Data Transfer Object (DTO)?
Think about how data is shared safely between parts of the app.
A DTO is used to safely transfer data between layers, like from the controller to the service, without exposing the full entity or database structure.
Choose the correct way to declare a simple DTO class in Spring Boot.
Remember Java class syntax requires access modifiers and method return types.
Option A correctly declares a public class with private field and public getter/setter methods with proper syntax.
Given this code snippet in a Spring Boot service, what will be the value of userDTO.getEmail() after execution?
User user = new User("Alice", "alice@example.com"); UserDTO userDTO = new UserDTO(); userDTO.setName(user.getName()); // Note: Email is not set in userDTO String email = userDTO.getEmail();
Check which fields are assigned values in the DTO.
The email field in userDTO was never set, so calling getEmail() returns null by default.
Consider this DTO class used in a Spring Boot REST controller. Why does it cause a JSON serialization error?
public class ProductDTO { private String name; private CategoryDTO category; // getters and setters } public class CategoryDTO { private String name; private ProductDTO product; // getters and setters }
Think about what happens when JSON tries to convert linked objects referencing each other.
The circular reference between ProductDTO and CategoryDTO causes infinite recursion during JSON serialization, leading to an error.
In a microservices architecture, why is using DTOs beneficial when services communicate?
Consider how data is shared efficiently and safely between independent services.
DTOs help by sending only necessary data, reducing payload size and keeping services independent by not exposing internal entities.