MapStruct 抛出异常

Mapstruct 映射器允许抛出特定的异常。 考虑一个自定义映射方法的情况,我们希望在出现无效数据的情况下抛出自定义异常。

语法

@Mapper(uses=DateMapper.class)
public interface UtilityMapper {
CarEntity getCarEntity(Car car) throws ParseException;
}

示例

映射 Enum 章节中更新的打开项目映射。

使用以下代码更新 UtilityMapper.java

UtilityMapper.java

package com.jiyik.mapper;

import com.jiyik.entity.CarEntity;
import com.jiyik.enums.OrderType;
import com.jiyik.enums.PlacedOrderType;
import com.jiyik.model.Car;
import org.mapstruct.MapMapping;
import org.mapstruct.Mapper;
import org.mapstruct.ValueMapping;

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;

@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

import com.jiyik.entity.CarEntity;
import com.jiyik.enums.OrderType;
import com.jiyik.enums.PlacedOrderType;
import com.jiyik.mapper.UtilityMapper;
import com.jiyik.model.Car;
import org.junit.Test;
import org.mapstruct.factory.Mappers;

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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

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

执行结果如下

MapStruct 抛出异常

查看笔记

扫码一下
查看教程更方便