在java中将一个pojo的所有属性复制到另一个pojo?

use*_*806 8 java java-ee-6

我有一些第三方罐子的POJO,我们不能直接向客户透露.

ThirdPartyPojo.java

public class ThirdPartyPojo implements java.io.Serializable {

    private String name;
    private String ssid;
    private Integer id;

    //public setters and getters

}
Run Code Online (Sandbox Code Playgroud)

上面的类是我们正在使用的第三方jar的一部分,如下所示.

ThirdPartyPojo result = someDao.getData(String id);
Run Code Online (Sandbox Code Playgroud)

现在我们的计划是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.

}
Run Code Online (Sandbox Code Playgroud)

现在我想知道是否有具有相同性质的最佳方式OurOwnPojo.javaThirdPartyPojo.java和填充数据ThirdPartyPojo.javaOurOwnPojo.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**

}
Run Code Online (Sandbox Code Playgroud)

谢谢!

Mas*_*dul 12

可能您正在搜索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;
}
Run Code Online (Sandbox Code Playgroud)