【问题标题】:MapStruct UnsupportedOperationException when mapped object is used to create a another mapped objectMapStruct UnsupportedOperationException 当映射对象用于创建另一个映射对象时
【发布时间】: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


    【解决方案1】:

    如果你的集合是不可变的,那么你应该使用CollectionMappingStrategy.TARGET_IMMUTABLE。这将向 MapStruct 发出信号,表明它不能使用集合/地图的 getter 来更新它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 2013-10-12
      相关资源
      最近更新 更多