Performance: Entity to DTO mapping
This affects the server response time and the size of data sent to the client, impacting page load speed and interaction responsiveness.
public List<UserDTO> getUsers() {
return userRepository.findAll().stream()
.map(user -> new UserDTO(user.getId(), user.getName()))
.collect(Collectors.toList());
}public List<User> getUsers() {
return userRepository.findAll();
}| Pattern | CPU Overhead | Payload Size | Serialization Time | Verdict |
|---|---|---|---|---|
| Direct entity return | Low | High | High | [X] Bad |
| Manual DTO mapping | Medium | Medium | Medium | [!] OK |
| Library-based DTO mapping | Low | Low | Low | [OK] Good |
| DTO with nested full collections | High | Very High | Very High | [X] Bad |
| DTO with summary fields only | Low | Low | Low | [OK] Good |
name to a DTO field fullName in Java?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());public UserDTO mapToDTO(UserEntity entity) {
UserDTO dto = new UserDTO();
dto.setName(entity.getFullName());
dto.setEmail(entity.getEmail());
return dto;
}UserEntity has a getName() method but no getFullName() method.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?Pass the test and this pattern moves unknown