在java中将一个pojo的所有属性复制到另一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20040800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
copying all properties of one pojo to another in java?
提问by user755806
I have below POJO of some third party jar which we cannot expose to our clients directly.
我有一些第三方 jar 的 POJO,我们不能直接向我们的客户公开。
ThirdPartyPojo.java
第三方Pojo.java
public class ThirdPartyPojo implements java.io.Serializable {
private String name;
private String ssid;
private Integer id;
//public setters and getters
}
Above class is part of third party jar which we are using as below.
上面的类是我们正在使用的第三方 jar 的一部分,如下所示。
ThirdPartyPojo result = someDao.getData(String id);
Now our plan is as ThirdPartyPojo
is part of third party jar, we cannot send ThirdPartyPojo
result type directly to clients. we want to create our own pojo which will have same properties as ThirdPartyPojo.java
class. we have to set the data from ThirdPartyPojo.java to OurOwnPojo.java
and return it as below.
现在我们的计划是ThirdPartyPojo
第三方 jar 的一部分,我们不能ThirdPartyPojo
直接将结果类型发送给客户。我们想创建我们自己的 pojo,它将与ThirdPartyPojo.java
类具有相同的属性。我们必须将来自 ThirdPartyPojo.java 的数据设置为OurOwnPojo.java
并返回如下。
public OurOwnPojo getData(String id){
ThirdPartyPojo result = someDao.getData(String id)
OurOwnPojo response = new OurOwnPojo(result);
return response;
//Now we have to populate above `result` into **OurOwnPojo** and return the same.
}
Now I want to know if there is a best way to have same properties in OurOwnPojo.java
as ThirdPartyPojo.java
and populate the data from ThirdPartyPojo.java
to OurOwnPojo.java
and return the same?
现在我想知道是否有最好的方法来在OurOwnPojo.java
as 中拥有相同的属性ThirdPartyPojo.java
并从ThirdPartyPojo.java
to填充数据OurOwnPojo.java
并返回相同的数据?
public class OurOwnPojo implements java.io.Serializable {
private ThirdPartyPojo pojo;
public OurOwnPojo(ThirdPartyPojo pojo){
this.pojo = pojo
}
//Now here i need to have same setter and getters as in ThirdPartyPojo.java
//i can get data for getters from **pojo**
}
Thanks!
谢谢!
回答by Masudul
Probably you are searching Apache Commons
BeanUtils.copyProperties.
可能您正在搜索Apache Commons
BeanUtils.copyProperties。
public OurOwnPojo getData(String id){
ThirdPartyPojo result = someDao.getData(String id);
OurOwnPojo myPojo=new OurOwnPojo();
BeanUtils.copyProperties(myPojo, result);
//This will copy all properties from thirdParty POJO
return myPojo;
}
回答by feuyeux
org.springframework.beans.BeanUtils is better than apache aone:
org.springframework.beans.BeanUtils 比 apache aone 好:
Task subTask = new Task();
org.springframework.beans.BeanUtils.copyProperties(subTaskInfo.getTask(), subTask);
回答by Zon
Don't misjudge origin and destination pojos:
不要误判起点和终点 pojo:
try {
BeanUtils.copyProperties(new DestinationPojo(), originPojo);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}