public static <T> T Convertjsonrequesttovo (httpservletrequest request, class<t> Voclass) throws Exception
{request.setcharacterencoding ("utf-8");
String requestbody = httpservletrequestutil.readhttpservletrequestbody (request);
map<string, object> Requestmap = (map<string, object>) json.parse (requestbody);
Return Convertmaptovo (Requestmap, Voclass);
public static <T> T Convertmaptovo (map<string, object> Map, class<t> voclass) throws Exception {
T obj = voclass.newinstance ();
if (Map.isempty ()) return to obj;
Beanwrapper beanwrapper = new Beanwrapperimpl (obj);
List<string> FieldList = Stream.of (Voclass.getdeclaredfields ()). Map (Field::getname)
. Collect (Collectors.tolist ());
Fieldlist.addall (Stream.of (Voclass.getsuperclass (). Getdeclaredfields ()). Map (Field::getname) . Collect (ColleCtors.tolist ())); For (map.entry<string, object> entry:map.entrySet ()) if (!
Stringutils.isempty (String.valueof (Entry.getvalue ())) if (Fieldlist.contains (Entry.getkey ()))
Beanwrapper.setpropertyvalue (Entry.getkey (), Entry.getvalue ());
return obj; }
Resources:
1, Java reflection to get the value of the parent class property
https://blog.csdn.net/Mingyueyixi/article/details/51164061
2, Java-reflection Reflection-get all fields including the parent class
https://blog.csdn.net/qq_32452623/article/details/54025185