
- MapStruct - Home
- MapStruct - Overview
- MapStruct - Environment Setup
- MapStruct - Mapping
- MapStruct - Basic Mapping
- MapStruct - Custom Mapping
- MapStruct - Mapping Multiple
- MapStruct - Mapping Nested Bean
- MapStruct - Mapping Direct Field
- MapStruct - Builder
- MapStruct - Data Type Conversions
- MapStruct - Implicit Type Conversion
- MapStruct - Using numberFormat
- MapStruct - Using dateFormat
- MapStruct - Using expression
- MapStruct - Using constant
- MapStruct - Using defaultValue
- MapStruct - Using defaultExpression
- MapStruct - Mapping Collections
- MapStruct - Mapping List
- MapStruct - Mapping Map
- Mapstruct - Miscellaneous
- MapStruct - Mapping Streams
- MapStruct - Mapping Enum
- MapStruct - Throwing Exceptions
- MapStruct - Customizing Mapper
- MapStruct - Useful Resources
- MapStruct - Quick Guide
- MapStruct - Useful Resources
- MapStruct - Discussion
MapStruct - Throwing Exception
Mapstruct mapper allows throwing specific exception. Consider a case of custom mapping method where we want to throw our custom exception in case of invalid data.
Syntax
@Mapper(uses=DateMapper.class) public interface UtilityMapper { CarEntity getCarEntity(Car car) throws ParseException; }
Following example demonstrates the same.
Example - Throwing exception
Open project mapping as updated in Mapping Enum chapter in Eclipse.
Update UtilityMapper.java with following code −
UtilityMapper.java
package com.tutorialspoint.mapper; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; 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.entity.CarEntity; import com.tutorialspoint.enums.OrderType; import com.tutorialspoint.enums.PlacedOrderType; import com.tutorialspoint.model.Car; @Mapper(uses=DateMapper.class) public interface UtilityMapper { @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); CarEntity getCarEntity(Car car) throws ParseException; } class DateMapper { public String asString(GregorianCalendar date) { return date != null ? new SimpleDateFormat( "yyyy-MM-dd" ) .format( date.getTime() ) : null; } public GregorianCalendar asDate(String date) throws ParseException { Date date1 = date != null ? new SimpleDateFormat( "yyyy-MM-dd" ) .parse( date ) : null; if(date1 != null) { return new GregorianCalendar(date1.getYear(), date1.getMonth(),date1.getDay()); } return null; } }
Update UtilityMapperTest.java with following code −
UtilityMapperTest.java
package com.tutorialspoint.test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.text.ParseException; 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.entity.CarEntity; import com.tutorialspoint.enums.OrderType; import com.tutorialspoint.enums.PlacedOrderType; import com.tutorialspoint.mapper.UtilityMapper; import com.tutorialspoint.model.Car; public class UtilityMapperTest { private UtilityMapper utilityMapper = Mappers.getMapper(UtilityMapper.class); @Test public void testMapMapping() { Map<Long, GregorianCalendar> source = new HashMap<>(); source.put(1L, new GregorianCalendar(2015, 3, 5)); Map<String, String> target = utilityMapper.getMap(source); assertEquals("2015-04-05", target.get("1")); } @Test public void testGetStream() { Stream<Integer> numbers = Arrays.asList(1, 2, 3, 4).stream(); Stream<String> strings = utilityMapper.getStream(numbers); assertEquals(4, strings.count()); } @Test public void testGetEnum() { PlacedOrderType placedOrderType = utilityMapper.getEnum(OrderType.EXTRA); PlacedOrderType placedOrderType1 = utilityMapper.getEnum(OrderType.NORMAL); PlacedOrderType placedOrderType2 = utilityMapper.getEnum(OrderType.STANDARD); assertEquals(PlacedOrderType.SPECIAL.name(), placedOrderType.name()); assertEquals(PlacedOrderType.NORMAL.name(), placedOrderType1.name()); assertEquals(PlacedOrderType.STANDARD.name(), placedOrderType2.name()); } @Test public void testGetCar() { Car car = new Car(); car.setId(1); car.setManufacturingDate("11/10/2020"); boolean exceptionOccured = false; try { CarEntity carEntity = utilityMapper.getCarEntity(car); } catch (ParseException e) { exceptionOccured = true; } assertTrue(exceptionOccured); } }
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] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.tutorialspoint.test.[1mCarMapperTest[m [INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.116 s -- in com.tutorialspoint.test.[1mCarMapperTest[m [INFO] Running com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m [INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 s -- in com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m [INFO] Running com.tutorialspoint.test.[1mStudentMapperTest[m [INFO] [1;32mTests run: [0;1;32m2[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 s -- in com.tutorialspoint.test.[1mStudentMapperTest[m [INFO] Running com.tutorialspoint.test.[1mUtilityMapperTest[m [INFO] [1;32mTests run: [0;1;32m4[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.022 s -- in com.tutorialspoint.test.[1mUtilityMapperTest[m [INFO] [INFO] Results: [INFO] [INFO] [1;32mTests run: 8, Failures: 0, Errors: 0, Skipped: 0[m [INFO] [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 3.808 s [INFO] Finished at: 2025-09-14T16:25:08+05:30 [INFO] [1m------------------------------------------------------------------------[m
Advertisements