简体   繁体   English

如何将属性从一个 Bean 复制到另一个并忽略嵌套对象的属性

[英]How to copy properties from one Bean to Another and ignore nested Object's property

I am working on a logic to copy properties from one bean to another bean in Java.我正在研究将属性从一个 bean 复制到 Java 中的另一个 bean 的逻辑。 I used BeanUtils to copy the properties and it works perfectly.我使用 BeanUtils 复制属性,它工作得很好。 Issue is that I want to ignore few properties of nested object but BeanUtils ignore properties work for only top level properties.问题是我想忽略嵌套 object 的几个属性,但 BeanUtils 忽略属性仅适用于顶级属性。 Is there any util or library to do that?有没有任何工具或库可以做到这一点? This is how my code is这就是我的代码

class A{
 private String name;
 private List<B> bArr;
 }

Class B {
int age;
int taxID;
}

So I want to hide taxID from list of bArr.所以我想从 bArr 列表中隐藏 taxID。 Is there a way to do this?有没有办法做到这一点?

you can use modelmapper and config as you want!!您可以根据需要使用模型映射器和配置!

you have several ways to configure it你有几种方法来配置它

<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>2.3.5</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM