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
Recall & Review
beginner
What is MapStruct in Spring Boot?
MapStruct is a code generator that simplifies mapping between Java objects, especially between DTOs and entities, by generating type-safe mappers at compile time.
Click to reveal answer
beginner
How does MapStruct improve mapping compared to manual mapping?
MapStruct generates mapping code automatically, reducing boilerplate, avoiding runtime errors, and improving performance by doing mapping at compile time instead of reflection.
Click to reveal answer
beginner
What annotation do you use to define a MapStruct mapper interface?
You use the @Mapper annotation on an interface to tell MapStruct to generate the implementation for mapping methods defined inside.
Click to reveal answer
intermediate
How do you get an instance of a MapStruct mapper in your Spring Boot application?
You can add componentModel = "spring" in @Mapper to make MapStruct generate a Spring bean, then inject it with @Autowired or constructor injection.
Click to reveal answer
intermediate
What happens if source and target fields have different names in MapStruct?
You can use @Mapping annotation on the method to specify how fields with different names map to each other, by defining source and target explicitly.
Click to reveal answer
What does MapStruct generate for you?
ADatabase schema migration scripts
BXML configuration files for mapping
CRuntime reflection-based mappers
DJava code for mapping between objects at compile time
✗ Incorrect
MapStruct generates Java code at compile time to map between objects, avoiding runtime reflection.
Which annotation marks an interface as a MapStruct mapper?
A@Mapper
B@Entity
C@Component
D@Service
✗ Incorrect
The @Mapper annotation tells MapStruct to generate the implementation for the interface.
How do you make a MapStruct mapper a Spring bean?
AAdd @Component on the interface
BAdd componentModel = "spring" in @Mapper
CUse @Autowired on the interface
DAdd @Service on the implementation
✗ Incorrect
Setting componentModel = "spring" in @Mapper makes MapStruct generate a Spring bean.
If source and target fields have different names, how do you map them?
AUse @Mapping annotation with source and target attributes
BMapStruct maps them automatically
CRename fields manually in both classes
DUse @Autowired to inject mapping
✗ Incorrect
You specify field mappings explicitly with @Mapping(source = ..., target = ...).
What is a main benefit of MapStruct over manual mapping?
AIt requires no annotations
BIt uses reflection at runtime
CIt generates mapping code at compile time for better performance
DIt only works with XML files
✗ Incorrect
MapStruct generates code at compile time, improving performance and safety.
Explain how MapStruct helps in mapping between DTOs and entities in Spring Boot.
Think about how MapStruct reduces manual coding and errors.
You got /4 concepts.
Describe how you handle mapping fields with different names using MapStruct.
Focus on the annotation that lets you define explicit field mappings.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using MapStruct in a Spring Boot application?
easy
A. To create user interface components
B. To manage database connections
C. To automatically map data between different object types
D. To handle HTTP requests and responses
Solution
Step 1: Understand MapStruct's role
MapStruct is a tool designed to copy data between objects automatically, reducing manual coding.
Step 2: Compare with other options
Options A, B, and C relate to other parts of Spring Boot, not object mapping.
Final Answer:
To automatically map data between different object types -> Option C
Quick Check:
MapStruct = automatic object mapping [OK]
Hint: MapStruct = automatic copying between objects [OK]
Common Mistakes:
Confusing MapStruct with database or web handling
Thinking MapStruct creates UI components
Assuming MapStruct manages HTTP requests
2. Which annotation is used to define a MapStruct mapper interface in Spring Boot?
easy
A. @Component
B. @Mapper
C. @Service
D. @Repository
Solution
Step 1: Identify the correct MapStruct annotation
MapStruct uses @Mapper to mark interfaces for automatic mapping generation.
Step 2: Understand Spring stereotypes
@Component, @Service, and @Repository are Spring annotations for beans but not for MapStruct mapping.
Final Answer:
@Mapper -> Option B
Quick Check:
MapStruct interface = @Mapper [OK]
Hint: MapStruct interfaces use @Mapper annotation [OK]
Common Mistakes:
Using @Component instead of @Mapper
Confusing Spring stereotypes with MapStruct annotations
What happens when you inject UserMapper in a Spring Boot service and call toDto(user)?
medium
A. It converts the User object to a UserDto automatically
B. It throws a NullPointerException because no implementation exists
C. It returns the original User object without changes
D. It requires manual implementation to work
Solution
Step 1: Understand componentModel = "spring"
This setting tells MapStruct to generate a Spring bean implementation automatically.
Step 2: Effect of calling toDto(user)
The generated implementation copies matching fields from User to UserDto automatically.
Final Answer:
It converts the User object to a UserDto automatically -> Option A
Quick Check:
componentModel spring = auto bean + mapping [OK]
Hint: componentModel spring means auto Spring bean mapper [OK]
Common Mistakes:
Thinking manual implementation is needed
Assuming it returns original object
Expecting runtime errors without implementation
4. Consider this mapper interface:
@Mapper
public interface ProductMapper {
ProductDto toDto(Product product);
}
When you try to inject ProductMapper in a Spring Boot service, you get an error. What is the likely cause?
medium
A. Missing componentModel = "spring" to register mapper as a Spring bean
B. The method name toDto is invalid
C. MapStruct does not support mapping Product to ProductDto
D. The interface must be a class, not an interface
Solution
Step 1: Check mapper registration in Spring context
Without componentModel = "spring", MapStruct does not create a Spring bean for the mapper.
Step 2: Understand injection failure
Spring cannot inject the mapper because it is not registered as a bean, causing an error.
Final Answer:
Missing componentModel = "spring" to register mapper as a Spring bean -> Option A
Quick Check:
Missing spring componentModel = no bean injection [OK]
Hint: Add componentModel spring to enable Spring bean injection [OK]
Common Mistakes:
Thinking method name causes error
Believing MapStruct can't map certain classes
Confusing interface with class requirement
5. You have two classes:
public class Employee {
private String name;
private int age;
private String department;
// getters and setters
}
public class EmployeeDto {
private String name;
private int age;
// getters and setters
}
You want to map Employee to EmployeeDto using MapStruct but ignore the department field. Which mapper method signature and annotation is correct?
hard
A. @Mapper
public interface EmployeeMapper {
EmployeeDto toDto(Employee employee);
}
B. @Mapper(componentModel = "spring")
public interface EmployeeMapper {
EmployeeDto toDto(Employee employee);
}
C. @Mapper
public interface EmployeeMapper {
@Mapping(source = "department", target = "department")
EmployeeDto toDto(Employee employee);
}
D. @Mapper(componentModel = "spring")
public interface EmployeeMapper {
@Mapping(target = "department", ignore = true)
EmployeeDto toDto(Employee employee);
}
Solution
Step 1: Identify ignoring a field in MapStruct
To ignore a field during mapping, use @Mapping(target = "fieldName", ignore = true) on the method.
Step 2: Check componentModel for Spring bean
Using componentModel = "spring" allows Spring to manage the mapper bean automatically.
Final Answer:
@Mapper(componentModel = "spring")
public interface EmployeeMapper {
@Mapping(target = "department", ignore = true)
EmployeeDto toDto(Employee employee);
} -> Option D
Quick Check:
Ignore field with @Mapping(target, ignore=true) + spring bean [OK]
Hint: Use @Mapping(target, ignore=true) to skip fields [OK]