java - Call custom methods using reflection in the setter methods of a jackson mapped class -
in spring application, receiving json request being mapped pojo using default deserializer. class looks below:
@jsonnaming(propertynamingstrategy.lowercasewithunderscoresstrategy.class) public class parammodel implements serializable { private string fieldone; private string fieldtwo; private string fieldthree; // setters , getters }
i performing custom validations (statically imported) might change values in setters:
public string getfieldone() { return fieldone; } public void setfieldone(string fieldone) { this.fieldone = fixfieldone(fieldone); } // similar getters , setters other fields
the validation method names in format fix{fieldname_in_pascalcase} fixfieldone().
i have class contains validation methods:
public class validate { public static string fixfieldone(string fieldone) { // stuff fieldone return fieldone; } // validation methods other fields }
i want writing custom deserializer:
@jsonnaming(propertynamingstrategy.lowercasewithunderscoresstrategy.class) @someannotation(using = validation.class) public class parammodel implements serializable { // fields, setters , getters // not calling validation methods explicitly setters anymore }
so that, call respective validation methods name (say can write class take name , resolve proper method name validation class) before setter sets value, , don't have manually call methods in each setters. because of time, setters , getters generated using auto completion tool provided ide, , gets tedious when fields keep increasing.
also, accepted practice in scenarios, when there around hundred fields.
Comments
Post a Comment