Bird
Raised Fist0
Spring Bootframework~20 mins

Entity to DTO mapping in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Entity to DTO Mapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this DTO mapping method?
Given the following Spring Boot entity and DTO classes, what will be the value of userDTO.getFullName() after calling mapToDTO(user)?
Spring Boot
public class User {
    private String firstName;
    private String lastName;
    private int age;

    // getters and setters
    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName) { this.firstName = firstName; }
    public String getLastName() { return lastName; }
    public void setLastName(String lastName) { this.lastName = lastName; }
    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
}

public class UserDTO {
    private String fullName;
    private int age;

    // getters and setters
    public String getFullName() { return fullName; }
    public void setFullName(String fullName) { this.fullName = fullName; }
    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
}

public UserDTO mapToDTO(User user) {
    UserDTO dto = new UserDTO();
    dto.setFullName(user.getFirstName() + " " + user.getLastName());
    dto.setAge(user.getAge());
    return dto;
}

// Usage:
User user = new User();
user.setFirstName("Jane");
user.setLastName("Doe");
user.setAge(30);
UserDTO userDTO = mapToDTO(user);
A"JaneDoe"
B"Jane Doe"
C"Doe Jane"
D"Doe"
Attempts:
2 left
💡 Hint
Look at how the fullName is constructed by concatenating firstName and lastName with a space.
📝 Syntax
intermediate
2:00remaining
Which option correctly maps a list of entities to a list of DTOs using Java streams?
You want to convert a List to List using Java streams in Spring Boot. Which code snippet correctly performs this mapping?
Spring Boot
List<User> users = ...; // list of User entities

// Map to List<UserDTO>
AList<UserDTO> dtos = users.stream().map(user -> mapToDTO(user)).collect(Collectors.toList());
BList<UserDTO> dtos = users.map(user -> mapToDTO(user)).toList();
CList<UserDTO> dtos = users.stream().map(mapToDTO).collect(Collectors.toList());
DList<UserDTO> dtos = users.stream().map(user -> mapToDTO).collect(Collectors.toList());
Attempts:
2 left
💡 Hint
Remember that List does not have a map method, streams do.
🔧 Debug
advanced
2:00remaining
Why does this mapping code throw a NullPointerException?
Consider this mapping method in a Spring Boot service: public UserDTO mapToDTO(User user) { UserDTO dto = new UserDTO(); dto.setFullName(user.getFirstName().toUpperCase() + " " + user.getLastName().toUpperCase()); dto.setAge(user.getAge()); return dto; } If user.getFirstName() is null, what happens when this method runs?
AIt throws a NullPointerException because calling toUpperCase() on null causes an error.
BIt compiles but skips setting fullName.
CIt returns a UserDTO with fullName as " " (empty string).
DIt returns a UserDTO with fullName as "null null".
Attempts:
2 left
💡 Hint
What happens if you call a method on a null object in Java?
🧠 Conceptual
advanced
2:00remaining
What is the main benefit of using DTOs instead of exposing entities directly in Spring Boot APIs?
Why do developers often map entities to DTOs before sending data in API responses?
ATo make the API responses larger and more detailed by including all entity fields.
BTo increase coupling between database schema and API contracts.
CTo avoid writing any mapping code and use entities directly everywhere.
DTo control and limit the data exposed to clients, improving security and decoupling internal models.
Attempts:
2 left
💡 Hint
Think about why you might not want to share your database structure directly with users.
state_output
expert
3:00remaining
What is the value of dto.getAddress().getCity() after mapping?
Given these classes and mapping method, what will dto.getAddress().getCity() return? public class User { private String name; private Address address; // getters/setters } public class Address { private String city; private String street; // getters/setters } public class UserDTO { private String name; private AddressDTO address; // getters/setters } public class AddressDTO { private String city; // getters/setters } public UserDTO mapToDTO(User user) { UserDTO dto = new UserDTO(); dto.setName(user.getName()); AddressDTO addrDto = new AddressDTO(); addrDto.setCity(user.getAddress().getCity()); dto.setAddress(addrDto); return dto; } // Usage: User user = new User(); user.setName("Alice"); Address addr = new Address(); addr.setCity("Springfield"); addr.setStreet("Main St"); user.setAddress(addr); UserDTO dto = mapToDTO(user);
A"Main St"
Bnull
C"Springfield"
DThrows NullPointerException
Attempts:
2 left
💡 Hint
Check how the city field is copied from Address to AddressDTO.

Practice

(1/5)
1. What is the main purpose of mapping an Entity to a DTO in Spring Boot?
easy
A. To speed up database queries
B. To separate database structure from data sent to clients
C. To store data in a different database
D. To automatically generate database tables

Solution

  1. Step 1: Understand Entity and DTO roles

    Entity represents database data, DTO is for data transfer outside the app.
  2. Step 2: Identify the purpose of mapping

    Mapping hides database details and controls what data is sent to clients.
  3. Final Answer:

    To separate database structure from data sent to clients -> Option B
  4. Quick Check:

    Entity to DTO mapping = data separation [OK]
Hint: Think: Entity is internal, DTO is external data format [OK]
Common Mistakes:
  • Confusing DTO with database storage
  • Thinking mapping speeds up queries
  • Assuming DTO changes database schema
2. Which of the following is the correct way to manually map an Entity field name to a DTO field fullName in Java?
easy
A. entity.getName(dto.setFullName());
B. entity.setName(dto.getFullName());
C. dto.getFullName(entity.setName());
D. dto.setFullName(entity.getName());

Solution

  1. Step 1: Identify source and target objects

    Entity is source, DTO is target for mapping.
  2. Step 2: Use getter on entity and setter on DTO

    Correct syntax is calling entity.getName() and passing to dto.setFullName().
  3. Final Answer:

    dto.setFullName(entity.getName()); -> Option D
  4. Quick Check:

    Getter from entity, setter on DTO [OK]
Hint: Getter from entity, setter on DTO for mapping [OK]
Common Mistakes:
  • Reversing source and target in mapping
  • Using setter as getter or vice versa
  • Calling methods with wrong parameters
3. Given the following code snippet, what will be the output of dto.getAge() after mapping?
public class UserEntity {
  private int age = 30;
  public int getAge() { return age; }
}

public class UserDTO {
  private int age;
  public void setAge(int age) { this.age = age; }
  public int getAge() { return age; }
}

UserEntity entity = new UserEntity();
UserDTO dto = new UserDTO();
dto.setAge(entity.getAge());
System.out.println(dto.getAge());
medium
A. 30
B. 0
C. null
D. Compilation error

Solution

  1. Step 1: Check initial value in Entity

    Entity's age is set to 30 by default.
  2. Step 2: Map Entity age to DTO and print

    DTO's age is set to entity.getAge(), so dto.getAge() returns 30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    Entity age 30 mapped to DTO age 30 [OK]
Hint: Mapping copies values exactly unless changed [OK]
Common Mistakes:
  • Assuming default int is null
  • Confusing getter/setter roles
  • Expecting compilation error without syntax issues
4. Identify the error in this manual mapping method:
public UserDTO mapToDTO(UserEntity entity) {
  UserDTO dto = new UserDTO();
  dto.setName(entity.getFullName());
  dto.setEmail(entity.getEmail());
  return dto;
}

Assuming UserEntity has a getName() method but no getFullName() method.
medium
A. Setter methods used incorrectly
B. Missing return statement
C. Calling non-existent method getFullName() on entity
D. DTO object not created

Solution

  1. Step 1: Check entity methods used

    Code calls entity.getFullName(), but entity only has getName().
  2. Step 2: Identify cause of error

    Calling a method that does not exist causes a compile-time error.
  3. Final Answer:

    Calling non-existent method getFullName() on entity -> Option C
  4. Quick Check:

    Method must exist on entity for mapping [OK]
Hint: Verify entity methods before calling in mapping [OK]
Common Mistakes:
  • Assuming method names match automatically
  • Ignoring compile errors from wrong method calls
  • Confusing getter names between classes
5. You want to map a list of UserEntity objects to a list of UserDTO objects using Java streams in Spring Boot. Which code snippet correctly performs this mapping assuming a method mapToDTO(UserEntity entity) exists?
hard
A. List<UserDTO> dtos = entities.stream().map(this::mapToDTO).collect(Collectors.toList());
B. List<UserDTO> dtos = entities.map(entity -> mapToDTO(entity));
C. List<UserDTO> dtos = entities.forEach(entity -> mapToDTO(entity));
D. List<UserDTO> dtos = entities.stream().forEach(this::mapToDTO);

Solution

  1. Step 1: Use stream() to process list

    entities.stream() creates a stream to transform elements.
  2. Step 2: Use map() to convert each entity to DTO

    map(this::mapToDTO) applies the mapping method to each element.
  3. Step 3: Collect results into a list

    collect(Collectors.toList()) gathers mapped DTOs into a list.
  4. Final Answer:

    List<UserDTO> dtos = entities.stream().map(this::mapToDTO).collect(Collectors.toList()); -> Option A
  5. Quick Check:

    Stream map + collect to list = correct mapping [OK]
Hint: Use stream().map(...).collect(toList()) for list mapping [OK]
Common Mistakes:
  • Using forEach instead of map for transformation
  • Calling map on list directly without stream()
  • Not collecting results after mapping