Copy all properties of one POJO to another in Java?

I have some POJOs of third-party cans, which we can't disclose directly to customers

ThirdPartyPojo. java

public class ThirdPartyPojo implements java.io.Serializable {

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

 //public setters and getters

}

The above class is part of the third-party jar we are using, as shown below

ThirdPartyPojo result = someDao.getData(String id);

Now our plan is that because thirdpartypojo is part of a third-party jar, we can't send thirdpartypojo result type directly to the client We want to create our own POJO, which will have the same function as thirdpartypojo Java class We must remove the data from thirdpartypojo Java is set to ourownpojo Java and return as follows

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 the best way in ourownpojo Java has the same function as thirdpartypojo Java, and the data from thirdpartypojo Java is populated into ourownpojo Java and return the same content?

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**

    }

thank you!

Solution

Maybe you are searching for 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;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>