【发布时间】:2021-11-29 23:38:12
【问题描述】:
当我使用 Mapper 并使用另一个映射对象作为变量时,生成的代码会抛出由这一行 redictionRequest.getAllFields().putAll(map) 引起的 UnsupportedOperationException
如果我使用非映射对象作为变量,则不会生成此代码。我找不到避免mapstruts生成此代码的方法。如果不是,则仅生成 predictionRequest.setDestination(protoDestination);
@Mapper(uses = {StringMapper.class, FloatMapper.class, DoubleMapper.class, BooleanMapper.class},
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, nullValueMappingStrategy = NullValueMappingStrategy.RETURN_NULL)
public interface DestinationMapper {
@Mapping(source = "request.countryIso", target = "marketIso", ignore = true)
@Mapping(source = "request.postalCode", target = "postalCode", ignore = true)
@Mapping(source = "timeZone", target = "marketTimeZone", ignore = true)
Destination map(Request request, String timeZone);
}
@Mapper(uses = {Destination.class, StringMapper.class, ProtoObjectMapper.class, FloatMapper.class, DoubleMapper.class, BooleanMapper.class}, nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
nullValueMappingStrategy = NullValueMappingStrategy.RETURN_NULL, collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface PredictionMapper {
@Mapping(source = "request.idOrder", target = "referenceId")
@Mapping(source = "request.orderDateUTC", target = "expeditionDate")
@Mapping(source = "protoDestination", target = "destination")
@Mapping(source = "notDeliverableLocalDates", target = "notDeliverableLocalDatesList")
PredictionRequest map(Request request, List<String> notDeliverableLocalDates, Destination protoDestination);
@Component
public class PredictionMapperImpl implements PredictionMapper {
@Override
public PredictionRequest map(Request request, List<String> notDeliverableLocalDates, Destination protoDestination) {
...............
if ( protoDestination != null ) {
predictionRequest.setDestination( protoDestination );
if ( protoDestination.getUnknownFields() != null ) {
predictionRequest.setUnknownFields( protoDestination.getUnknownFields() );
}
if ( predictionRequest.getAllFields() != null ) {
Map<FieldDescriptor, Object> map = protoDestination.getAllFields();
if ( map != null ) {
predictionRequest.getAllFields().putAll( map ); //**Throw java.lang.UnsupportedOperationException**
}
}
}
return predictionRequest.build();
}
【问题讨论】:
标签: mapstruct