Challenge - 5 Problems
MapStruct Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this MapStruct mapping?
Given the following MapStruct mapper interface and classes, what will be the value of
target.name after mapping?Spring Boot
public class Source { private String name; public Source(String name) { this.name = name; } public String getName() { return name; } } public class Target { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } } @Mapper public interface MyMapper { MyMapper INSTANCE = Mappers.getMapper(MyMapper.class); Target sourceToTarget(Source source); } // Usage: Source source = new Source("Alice"); Target target = MyMapper.INSTANCE.sourceToTarget(source); String result = target.getName();
Attempts:
2 left
💡 Hint
MapStruct automatically maps properties with the same name and compatible types.
✗ Incorrect
MapStruct generates an implementation that copies the value of the 'name' property from Source to Target. Since the source has 'Alice', the target's name will be 'Alice'.
📝 Syntax
intermediate1:30remaining
Which MapStruct annotation is required to define a mapper interface?
You want to create a MapStruct mapper interface to convert between two classes. Which annotation must you use on the interface?
Attempts:
2 left
💡 Hint
MapStruct uses a special annotation to generate mapper implementations.
✗ Incorrect
The @Mapper annotation tells MapStruct to generate an implementation for the interface.
🔧 Debug
advanced2:30remaining
Why does this MapStruct mapping fail to compile?
Consider this mapper interface:
@Mapper
public interface UserMapper {
UserDto toDto(User user);
}
User and UserDto have fields with different names: User has 'firstName', UserDto has 'givenName'. No additional configuration is provided.
Why does MapStruct fail to generate the implementation?
Attempts:
2 left
💡 Hint
MapStruct matches fields by name by default.
✗ Incorrect
MapStruct matches fields by name. If names differ, you must specify mappings with @Mapping annotations.
❓ state_output
advanced2:00remaining
What is the output of this MapStruct mapping with nested objects?
Given these classes:
public class Address {
private String city;
public Address(String city) { this.city = city; }
public String getCity() { return city; }
}
public class User {
private String name;
private Address address;
public User(String name, Address address) { this.name = name; this.address = address; }
public String getName() { return name; }
public Address getAddress() { return address; }
}
public class UserDto {
private String name;
private String city;
public void setName(String name) { this.name = name; }
public void setCity(String city) { this.city = city; }
public String getName() { return name; }
public String getCity() { return city; }
}
@Mapper
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
@Mapping(source = "address.city", target = "city")
UserDto userToUserDto(User user);
}
// Usage:
User user = new User("Bob", new Address("Paris"));
UserDto dto = UserMapper.INSTANCE.userToUserDto(user);
String output = dto.getCity();
Attempts:
2 left
💡 Hint
MapStruct supports nested property mapping with dot notation.
✗ Incorrect
The @Mapping annotation tells MapStruct to map user.getAddress().getCity() to dto.setCity(). The value is "Paris".
🧠 Conceptual
expert3:00remaining
Which statement about MapStruct's generated mapper implementations is true?
Choose the correct statement about how MapStruct generates mapper implementations in a Spring Boot project.
Attempts:
2 left
💡 Hint
Think about when MapStruct creates the code for mapping.
✗ Incorrect
MapStruct generates Java code during compilation, avoiding runtime reflection and improving performance.