如果shipping.status==Status.Opened
,我应该将ShipmentResponseDto.cargo.packaging.lastMeasurements
中的几个字段返回为null
,如下所示:
if(shipment.status=Status.Opened){
ShipmentResponseDto.cargo.packaging.lastMeasurements.firstField = null;
ShipmentResponseDto.cargo.packaging.lastMeasurements.secondField = null;
}
我想在mapstruct中完成,整个问题是答案是由两个Map器组装而成的,我没有成功地实现这一点。
@Mapper(uses = {
CargoPackagingMapper.class
})
public interface ShipmentMapper extends BasicMapper<ShipmentResponseDto, Shipment> {
}
@Mapper(uses = {PackagingTypeMapper.class})
public abstract class CargoPackagingMapper implements BasicMapper<CargoPackagingResponseDto, Packaging> {
@Override
@Mappings(...)
public abstract CargoPackagingResponseDto toDto(Packaging source);
}
public class Shipment {
...
...
@Enumerated(EnumType.STRING)
private Status status;
private Set<ShipmentCargo> cargo = new HashSet<>();
...
}
public class ShipmentCargo {
...
@ManyToOne
private Packaging packaging;
...
}
public class Packaging {
...
private PackagingLastMeasurements lastMeasurements;
...
}
1条答案
按热度按时间xe55xuns1#
一个简单的解决方案是在
ShipmentMapper
中定义@AfterMapping
方法,将这些字段设置为null
,如下所示: