如何配置模型映射器以跳过目标中的嵌套属性

发布于 2025-01-09 20:46:46 字数 1056 浏览 1 评论 0 原文

Source{
String one;
String two;
}

Destination{
String one;
Child child;
}

Child{
String two;
}

main() {
ModelMapper modelMapper = new ModelMapper();
// what configurations to set here?
Source source = new Source("one=1", "two=2");
Destination desiredResult = new Destination();
desiredResult.setOne("one=1");
Destination mappedResult = modelmapper.map(source, Destination.class )
assertEquals(mappedResult,  desiredResult );
}

问题是 modelmapper 将映射 source.two =>; target.child.two,我不想要。我尝试过类似的方法,但

1. modelMapper.getConfiguration().setPreferNestedProperties(false);
2. modelMapper.getConfiguration().setAmbiguityIgnored(true);
3. modelMapper.getConfiguration().setImplicitMappingEnabled(false);
4. modelMapper.addMappings(new PropertyMap<Source, Destination>() {
      @Override
      protected void configure() {
        skip(destination.getChild());
        skip(destination.getChild().getTwo());
      }
    });

没有成功。

我希望有一个通用的解决方案,可以阻止模型映射器映射目标对象中的任何嵌套对象的属性。

Source{
String one;
String two;
}

Destination{
String one;
Child child;
}

Child{
String two;
}

main() {
ModelMapper modelMapper = new ModelMapper();
// what configurations to set here?
Source source = new Source("one=1", "two=2");
Destination desiredResult = new Destination();
desiredResult.setOne("one=1");
Destination mappedResult = modelmapper.map(source, Destination.class )
assertEquals(mappedResult,  desiredResult );
}

The problem is that modelmapper will map source.two => destination.child.two, which I do not want. I have tried methods like

1. modelMapper.getConfiguration().setPreferNestedProperties(false);
2. modelMapper.getConfiguration().setAmbiguityIgnored(true);
3. modelMapper.getConfiguration().setImplicitMappingEnabled(false);
4. modelMapper.addMappings(new PropertyMap<Source, Destination>() {
      @Override
      protected void configure() {
        skip(destination.getChild());
        skip(destination.getChild().getTwo());
      }
    });

None worked.

I hope to have a general solution that can stop model mapper to map any nested object's properties in destination object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离线来电— 2025-01-16 20:46:46

您是否可能缺少一些预先存在的配置?你的例子对我来说效果很好。

无论如何,当您使用LOOSE匹配策略时,这种情况大多数都会发生。

modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);

您可以通过将匹配策略显式设置为 STANDARDSTRICT 来修复此问题。

modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STANDARD);

或者

modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

检查docs 了解有关匹配策略、命名约定、默认配置等的更多信息。

Could you perhaps be missing some preexisting configuration? Your example works just fine for me.

Anyway, such things happen mostly when you are using LOOSE matching strategy.

modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);

You can fix it by explicitly setting matching strategy to STANDARD or STRICT.

modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STANDARD);

Or

modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

Check the docs for additional information on matching strategies, naming conventions, default configurations, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文