ModelMapper - how calculate a value from source and set it to destination

岁酱吖の 提交于 2021-01-29 13:54:52

问题


I'm trying to use ModelMapper for mapping source to destination.

In my specific case, in the source class there is a property (List of Review), where I have to sum by rating and set this value to destination class.

So I've tried with converter, but it doesn't working.

        Converter<List<Review>, Double> sumReviewsRating = new AbstractConverter<List<Review>, Double>() {
        @Override
        protected Double convert(List<Review> reviews) {
            if(reviews!=null){
                return reviews.stream().mapToDouble(Review::getRating).sum();
                //it doesn't work also with return 1.0 for example;
            }else{
                return 0.0;
            }
        }
    };

 modelMapper.typeMap(MySource.class, MyDestination.class).addMappings(mapper ->{
            mapper.map(MySource::getCoverImageId, MyDestination::setImageUrl); //this works
            mapper.using(sumReviewsRating).map(MySource::getReviews, MyDestination::setRating);
        });

Stacktrace:

org.modelmapper.internal.ErrorsException: null
    at org.modelmapper.internal.Errors.toException(Errors.java:254) ~[modelmapper-2.3.6.jar:na]
    at org.modelmapper.internal.ReferenceMapExpressionImpl.map(ReferenceMapExpressionImpl.java:71) ~[modelmapper-2.3.6.jar:na]
    at org.modelmapper.internal.ConfigurableConditionExpressionImpl.map(ConfigurableConditionExpressionImpl.java:65) ~[modelmapper-2.3.6.jar:na]

If I put a breakpoint in the converter, it doesn't enter.

Where is my mystake?


回答1:


The Following worked for me :

ModelMapper modelMapper = new ModelMapper();

Converter<List<String>, Double> sumReviewsRating = new AbstractConverter<List<String>, Double>() {
      @Override
      protected Double convert(List<String> reviews) {
        if(reviews!=null){
          return 1.0;
          //it work also with return 1.0 for example;
        }else{
          return 0.0;
        }
      }
    };

    modelMapper.typeMap(MySource.class, MyDestination.class).addMappings(mapper ->{
      mapper.using(sumReviewsRating).map(MySource::getReview, MyDestination::setRating);
    });

    MySource mySource = new MySource();
    mySource.setReview(Arrays.asList(new String[]{"1","2"}));
    MyDestination destination = modelMapper.map(mySource,MyDestination.class);

    System.out.println(destination.getRating());

Classes

@Getter @Setter
public class MyDestination {
  Double rating;
}


@Getter @Setter
public class MySource {
  List<String> review;
}

The above code successufully did the conversion.

Try adding multiple mappings like this

**

typeMap.addMappings(mapper -> mapper.<String>map(Src::getA, Dest::setB))
          .addMappings(mapper -> mapper.<String>skip(Dest::setB))

**



来源:https://stackoverflow.com/questions/59610414/modelmapper-how-calculate-a-value-from-source-and-set-it-to-destination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!