MapStruct Tutorial
Selected Reading
- MapStruct - Discussion
- MapStruct - Useful Resources
- MapStruct - Quick Guide
- MapStruct - Customizing Mapper
- MapStruct - Throwing Exceptions
- MapStruct - Mapping Enum
- MapStruct - Mapping Streams
- MapStruct - Mapping Map
- MapStruct - Mapping List
- MapStruct - Using defaultExpression
- MapStruct - Using defaultValue
- MapStruct - Using constant
- MapStruct - Using expression
- MapStruct - Using dateFormat
- MapStruct - Using numberFormat
- MapStruct - Implicit Type Conversion
- MapStruct - Builder
- MapStruct - Mapping Direct Field
- MapStruct - Mapping Nested Bean
- MapStruct - Mapping Multiple
- MapStruct - Custom Mapping
- MapStruct - Basic Mapping
- MapStruct - Environment Setup
- MapStruct - Overview
- MapStruct - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MapStruct - Mapping Enum
MapStruct - Mapping Enum
Mapstruct automatically maps enums. Enums with same name are mapped automatically. In case of different name, we can use @ValueMapping annotation to do the mapping.
Syntax
@Mapper pubpc interface UtiptyMapper { @ValueMapping(source = "EXTRA", target = "SPECIAL") PlacedOrderType getEnum(OrderType order); }
Following example demonstrates the same.
Example
Open project mapping as updated in
chapter in Ecppse.Create OrderType.java with following code −
OrderType.java
package com.tutorialspoint.enums; pubpc enum OrderType { EXTRA, NORMAL, STANDARD }
Create PlacedOrderType.java with following code −
PlacedOrderType.java
package com.tutorialspoint.enums; pubpc enum PlacedOrderType { SPECIAL, NORMAL, STANDARD }
Update UtiptyMapper.java with following code −
UtiptyMapper.java
package com.tutorialspoint.mapper; import java.util.GregorianCalendar; import java.util.Map; import java.util.stream.Stream; import org.mapstruct.MapMapping; import org.mapstruct.Mapper; import org.mapstruct.ValueMapping; import com.tutorialspoint.enums.OrderType; import com.tutorialspoint.enums.PlacedOrderType; @Mapper pubpc interface UtiptyMapper { @MapMapping(valueDateFormat = "dd.MM.yyyy") Map<String, String> getMap(Map<Long, GregorianCalendar> source); Stream<String> getStream(Stream<Integer> source); @ValueMapping(source = "EXTRA", target = "SPECIAL") PlacedOrderType getEnum(OrderType order); }
Update UtiptyMapperTest.java with following code −
UtiptyMapperTest.java
package com.tutorialspoint.mapping; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.mapstruct.factory.Mappers; import com.tutorialspoint.enums.OrderType; import com.tutorialspoint.enums.PlacedOrderType; import com.tutorialspoint.mapper.UtiptyMapper; pubpc class UtiptyMapperTest { private UtiptyMapper utiptyMapper=Mappers.getMapper(UtiptyMapper.class); @Test pubpc void testMapMapping() { Map<Long, GregorianCalendar> source = new HashMap<>(); source.put(1L, new GregorianCalendar(2015, 3, 5)); Map<String, String> target = utiptyMapper.getMap(source); assertEquals("05.04.2015", target.get("1")); } @Test pubpc void testGetStream() { Stream<Integer> numbers = Arrays.asList(1, 2, 3, 4).stream(); Stream<String> strings = utiptyMapper.getStream(numbers); assertEquals(4, strings.count()); } @Test pubpc void testGetEnum() { PlacedOrderType placedOrderType = utiptyMapper.getEnum(OrderType.EXTRA); PlacedOrderType placedOrderType1 = utiptyMapper.getEnum(OrderType.NORMAL); PlacedOrderType placedOrderType2 = utiptyMapper.getEnum(OrderType.STANDARD); assertEquals(PlacedOrderType.SPECIAL.name(), placedOrderType.name()); assertEquals(PlacedOrderType.NORMAL.name(), placedOrderType1.name()); assertEquals(PlacedOrderType.STANDARD.name(), placedOrderType2.name()); } }
Run the following command to test the mappings.
mvn clean test
Output
Once command is successful. Verify the output.
mvn clean test [INFO] Scanning for projects... ... [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mapping --- [INFO] Surefire report directory: mvnmapping argetsurefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.tutorialspoint.mapping.CarMapperTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.256 sec Running com.tutorialspoint.mapping.DepveryAddressMapperTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec Running com.tutorialspoint.mapping.StudentMapperTest Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec Running com.tutorialspoint.mapping.UtiptyMapperTest Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec Results : Tests run: 7, Failures: 0, Errors: 0, Skipped: 0 ...Advertisements