Mapstruct';后映射';不打电话

Mapstruct';后映射';不打电话,mapstruct,Mapstruct,问题是,用@AfterMapping注释的方法根本不被调用。从testToEntityMapping转到toEntity方法,但它不调用任何toEntityAfterMapping()方法。为什么?可能吗?如何使用MapStruct实现这一点 (这里我准备了一个毫无意义的场景,但它完全抓住了我问题的本质) 实体: DTO: 制图员: public abstract class FordMapper<D extends FordDto> { public Ford toEnti

问题是,用
@AfterMapping
注释的方法根本不被调用。从
testToEntityMapping
转到
toEntity
方法,但它不调用任何
toEntityAfterMapping()
方法。为什么?可能吗?如何使用MapStruct实现这一点

(这里我准备了一个毫无意义的场景,但它完全抓住了我问题的本质)
实体:

DTO:

制图员:

public abstract class FordMapper<D extends FordDto> {
    public Ford toEntity(D dto) {

        /* fill in fields common to both ford versions */

        final Ford ford = new Ford();

        ford.setColor(dto.getColor());
        ford.setMarket(dto.getMarket());

        return ford;
    }
}
@Mapper(componentModel = "spring")
public abstract class EuropeanFordMapper extends FordMapper<EuropeanFordDto> {

    @AfterMapping
    public void toEntityAfterMapping(final EuropeanFordDto dto, @MappingTarget final Ford entity) {

        /* Fill in fields related to european version of the ford */

        entity.setTotalWidth(dto.getTotalWidth());
    }
}
@Mapper(componentModel = "spring")
public abstract class AmericanFordMapper extends FordMapper<AmericanFordDto> {

    @AfterMapping
    public void toEntityAfterMapping(final AmericanFordDto dto, @MappingTarget final Ford entity) {

        /* Fill in fields related to american version of the ford */

        entity.setTotalWidth(dto.getTotalWidth());
    }
}

好吧,这比我想象的要简单

映射器的公共接口{ @映射(target=“totalWidth”,ignore=true) 福特实体公开摘要(D dto); }
您甚至可以查看
toEntity()
方法中的实现,有一个对
toEntityAfterMapping()
的调用,因此一切都是正确的,并且符合我们期望的结果。

我对同一问题的解决方案是,我忘了在@AfterMapping方法中添加“default”关键字(我使用了接口)。之后,在生成的代码中出现了方法


如果您是Mapstruct新手,请不要忘记在进行更改后进行mvn/gradle清理和编译。

我完全没有注意到您的方法在
FordMapper
中不是抽象的。我会编辑这个问题,然后使用第二种方法,即MapStruct方法。你甚至可以把你的
抽象类
变成
接口
我已经编辑了这个问题。希望它像你希望的那样。干杯
public abstract class FordDto {
    public String market;
    public String color;

    //getters and setters omitted for brevity
}

public class EuropeanFordDto extends FordDto{
    private int totalWidth;

    public int getTotalWidth() {
        return totalWidth + 2;//"+2" for EU market
    }
    //setter omitted for brevity
}

public class AmericanFordDto extends FordDto{
    private int totalWidth;

    public int getTotalWidth() {
        return totalWidth + 1;//"+1" for US market
    }

    //setter omitted for brevity
}
public abstract class FordMapper<D extends FordDto> {
    public Ford toEntity(D dto) {

        /* fill in fields common to both ford versions */

        final Ford ford = new Ford();

        ford.setColor(dto.getColor());
        ford.setMarket(dto.getMarket());

        return ford;
    }
}
@Mapper(componentModel = "spring")
public abstract class EuropeanFordMapper extends FordMapper<EuropeanFordDto> {

    @AfterMapping
    public void toEntityAfterMapping(final EuropeanFordDto dto, @MappingTarget final Ford entity) {

        /* Fill in fields related to european version of the ford */

        entity.setTotalWidth(dto.getTotalWidth());
    }
}
@Mapper(componentModel = "spring")
public abstract class AmericanFordMapper extends FordMapper<AmericanFordDto> {

    @AfterMapping
    public void toEntityAfterMapping(final AmericanFordDto dto, @MappingTarget final Ford entity) {

        /* Fill in fields related to american version of the ford */

        entity.setTotalWidth(dto.getTotalWidth());
    }
}
@Service
public class CarService {

    @Autowired
    private AmericanFordMapper americanFordMapper;
    @Autowired
    private EuropeanFordMapper europeanFordMapper;

    public void testToEntityMapping(final FordDto dto) {

        if (dto instanceof AmericanFordDto) {
            americanFordMapper.toEntity((AmericanFordDto) dto);
        } else {
            europeanFordMapper.toEntity((EuropeanFordDto) dto);
        }
    }
}