java - Best practice generating object for response -
i have rest server spring. there lot of requests 1 of params fields
fields set of fields server should return in response. like: /?fields=[id,name]
, server should return json object both fields know best practice generating such response. this:
private map<string, object> processbook(bookentity book, set<string> fields, string locale){ map<string, object> map = new hashmap<string, object>(); //.. if(fields.contains(id)){ map.put(id, book.getid()); } if(fields.contains(isbn)){ map.put(isbn, book.getisbn()); } if(fields.contains(description)){ if(locale.equals(userlocale.uk)) map.put(description, book.getdescriptionua()); else if(locale.equals(userlocale.ru)) map.put(description, book.getdescriptionru()); else map.put(description, book.getdescriptionen()); } //.. return map; }
maybe there better alternative?
note in case obtain data db - filled bookentity object, , show requested fields.
in opinion it'd "much better alternative" delegate field list appropriate downstream integration call , bookentity object necessary fields. mentioned above method reduce 1 line, db responses more lightweight, bring simplicity , optimization gain system.
any adequate db provides such functionality: sql or nosql, etc.
p.s. plus standard approach of object json mapping such jackson or gson @ top level.
Comments
Post a Comment