On server I have something like this
public class Instrument {
private String myPropOne;
private String myPropTwo;
private String myPropThree;
public String getMyPropOne() {
return myPropOne;
}
public void setMyPropOne(String myPropOne) {
this.myPropOne = myPropOne;
}
public String getMyPropTwo() {
return myPropTwo;
}
public void setMyPropTwo(String myPropTwo) {
this.myPropTwo = myPropTwo;
}
public String getMyPropThree() {
return myPropThree;
}
public void setMyPropThree(String myPropThree) {
this.myPropThree = myPropThree;
}
}
and on Browser side it a map is to be sent
{ my_prop_one : 'val1', my_prop_two : 'val2', my_prop_three : 'val3'}
One way is to create define toMap() methods and put all the properties one by one and do the same when it is coming from UI-side something like fromMap()
Now question is I want write some common function
set('my_prop_one', 'val2')
so that it will look up the instance and set correct value, i will be extending this class to create more instruments types with different properties I can do this using annotations
public class Instrument {
private String myPropOne;
private String myPropTwo;
private String myPropThree;
@KeyMap(value="my_prop_one")
public String getMyPropOne() {
return myPropOne;
}
@KeyMap(value="my_prop_one")
public void setMyPropOne(String myPropOne) {
this.myPropOne = myPropOne;
}
@KeyMap(value="my_prop_two")
public String getMyPropTwo() {
return myPropTwo;
}
@KeyMap(value="my_prop_two")
public void setMyPropTwo(String myPropTwo) {
this.myPropTwo = myPropTwo;
}
@KeyMap(value="my_prop_three")
public String getMyPropThree() {
return myPropThree;
}
@KeyMap(value="my_prop_three")
public void setMyPropThree(String myPropThree) {
this.myPropThree = myPropThree;
}
}
I will have to write annotations twice. is there any better way to do this? Some thing like I add annotations on properties and getter/setter get somehow linked and can be used to extract properties, some way to create a hashMap?
JsonUtil I am already using gives me this out put
{ myPropOne : 'val1', myPropTwo : 'val2', myPropThree : 'val3'}
But again setter/getter for one property? how to do that? EDIT 3 :
@JsonAnyGetter & @JsonAnySetter
like explained in example here But they work on only map not real properties which is my requirement.