java - Spring Jackson JsonViews - Get Fields based on JsonView -
i've got entity number of fields , jsonview specified:
public class client { @jsonview(views.clientview.class) @column(name = "clientid") private long clientid; @jsonview(views.clientview.class) @column(name = "name") private string name @jsonview(views.systemview.class) @column(name = "istest") private boolean istest; ......... }
the views defined follows:
public class views { public interface systemview extends clientview { } public interface clientview { } }
i have simple controller in order update client. since field istest
set systemview
, don't want client update field.
i've read in order posts, has done manually first loading client , updating parameters accordingly (in case clientid
, name
).
now want list of fields need update (i.e. fields marked jsonview
views.clientview.class
). i've tried following it's not working:
objectreader reader = objectmapper.readerwithview(systemview.class); contextattributes attributes = reader.getattributes();
but, attributes
returning without elements.
is there way list of fields based on view?
you can try access , inspect annotations of fields in class reflection
likes:
list<field> annotatedfields = new arraylist<>(); field[] fields = client.class.getdeclaredfields(); (field field : fields) { if (!field.isannotationpresent(jsonview.class)) { continue; } jsonview annotation = field.getannotation(jsonview.class); if (arrays.aslist(annotation.value()).contains(views.systemview.class)) { annotatedfields.add(field); } }
in above example, annotatedfields
contain list of fields in client
class annotated jsonview
value contains views.systemview.class
.
Comments
Post a Comment