Java ModelMapper-如何从源计算值并将其设置为目标
我正在尝试使用ModelMapper将源映射到目标 在我的特定情况下,在源类中有一个属性(审阅列表),在这里我必须按评级求和,并将该值设置为目标类 所以我试过使用转换器,但它不起作用Java ModelMapper-如何从源计算值并将其设置为目标,java,spring-boot,modelmapper,Java,Spring Boot,Modelmapper,我正在尝试使用ModelMapper将源映射到目标 在我的特定情况下,在源类中有一个属性(审阅列表),在这里我必须按评级求和,并将该值设置为目标类 所以我试过使用转换器,但它不起作用 Converter<List<Review>, Double> sumReviewsRating = new AbstractConverter<List<Review>, Double>() { @Override p
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);
});
如果我在转换器中设置断点,它不会进入
我的人生目标在哪里?以下几点对我很有用:
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());
ModelMapper ModelMapper=newmodelmapper();
Converter SumReviewRating=新的AbstractConverter(){
@凌驾
受保护的双重转换(列表审查){
如果(评论!=null){
返回1.0;
//例如,它也适用于return1.0;
}否则{
返回0.0;
}
}
};
typeMap(MySource.class,MyDestination.class).addMappings(mapper->{
使用(SumReviewRating).map(MySource::getReview,MyDestination::setRating);
});
MySource MySource=新的MySource();
setReview(Arrays.asList(新字符串[]{“1”,“2”}));
MyDestination=modelMapper.map(mySource,MyDestination.class);
System.out.println(destination.getRating());
班级
@Getter @Setter
public class MyDestination {
Double rating;
}
@Getter @Setter
public class MySource {
List<String> review;
}
@Getter@Setter
公共类MyDestination{
双重评级;
}
@Getter@Setter
公共类MySource{
清单审查;
}
上面的代码成功地完成了转换
尝试像这样添加多个映射
**
typeMap.addMappings(mapper->mapper.map(Src::getA,Dest::setB))
.addMappings(映射器->映射器.skip(Dest::setB))
**您的代码似乎很好。我想你只是有一些
评论。评分=null
?也尝试添加检查。@pirho是的,我如何检查并忽略它们?请添加多一点堆栈跟踪。它是否曾经进入转换器?您在问题中说过:如果我在转换器中放置断点,它不会输入是的,我在另一个项目中再次尝试,它可以工作,但是如果我在我的方法中尝试,它不会工作:-(我可以有一个更具体的错误,而不是通用的org.modelmapper.internal.ErrorsException吗?
@Getter @Setter
public class MyDestination {
Double rating;
}
@Getter @Setter
public class MySource {
List<String> review;
}
typeMap.addMappings(mapper -> mapper.<String>map(Src::getA, Dest::setB))
.addMappings(mapper -> mapper.<String>skip(Dest::setB))