
- MapStruct 教程
- MapStruct - 主頁
- MapStruct - 概述
- MapStruct - 環境設定
- 對映
- MapStruct - 基本對映
- MapStruct - 自定對映
- MapStruct - 多重對映
- MapStruct - 對映巢狀 Bean
- MapStruct - 對映直接欄位
- MapStruct - 構建器
- 資料型別轉換
- MapStruct - 隱式型別轉換
- MapStruct - 使用 numberFormat
- MapStruct - 使用 dateFormat
- MapStruct - 使用表示式
- MapStruct - 使用常量
- MapStruct - 使用 defaultValue
- MapStruct - 使用 defaultExpression
- 對映集合
- MapStruct - 對映列表
- MapStruct - 對映對映
- 其他
- MapStruct - 對映流
- MapStruct - 對映列舉
- MapStruct - 丟擲異常
- MapStruct - 自定義對映器
- MapStruct 有用資源
- MapStruct - 快速指南
- MapStruct - 有用資源
- MapStruct - 討論
MapStruct - 丟擲異常
MapStruct 對映器允許丟擲特定異常。考慮一種自定對映方法的情況,在這種情況下,如果資料無效,我們希望丟擲我們的自定異常。
語法
@Mapper(uses=DateMapper.class) public interface UtilityMapper { CarEntity getCarEntity(Car car) throws ParseException; }
以下示例演示了這一點。
示例
在 Eclipse 中開啟更新的對映專案,如 對映列舉 一章中所述。
使用以下程式碼更新 UtilityMapper.java −
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; } }
使用以下程式碼更新 UtilityMapperTest.java −
UtilityMapperTest.java
package com.tutorialspoint.mapping; 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); } }
執行以下命令來測試對映。
mvn clean test
輸出
命令成功後,驗證輸出。
mvn clean test [INFO] Scanning for projects... ... [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mapping --- [INFO] Surefire report directory: \mvn\mapping\target\surefire-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.DeliveryAddressMapperTest 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.UtilityMapperTest Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec Results : Tests run: 8, Failures: 0, Errors: 0, Skipped: 0 ...
廣告