dimanche 22 février 2015

Mapping properties of different classes in Spring

I have a class as below:



class Original{
String sku;
String productname;
// few other properties
// getter / setter methods
}


I populate these properties by calling an api (which returns a json) using RestTemplate. The problem is, based on some condition, I call 3 different apis. Now, all three apis have the information I need. However, the keys might be different in different apis. For instance, instead of "productname", the key might just be "name".


So, I have created three different classes, each class mapping to an api. Eg:



class Api1{
String sku;
String name;
// other properties and getter/setters
}


I populate the values of Api1 using something like this:



Api1 api1 = restTemplate.exchange(url, HttpMethod.GET, request, Api1.class).getBody();


Now, I need to map the properties of the new classes back to the original class (i.e map "name" of Api1 class to "productname" of Original class). How do I do this? Do I need to do the mapping manually? Or does Spring offer a simpler solution for this?


Aucun commentaire:

Enregistrer un commentaire