return dynamic value in java REST / Spring / Hibernate -
i've inherited rest api using jboss, spring, hibernate, etc. , i'm new these please forgive seems dumb question (to me).
the basic flow when performing build entity list add modelandview object return it.
question: how can add calculated value response?
i don't want store calculated values in database, nor want add member each entity being returned (ie arraylist<athletes>
, add "points behind leader" member athletes class). want perform calculation on each request , append member json response dynamic value so:
{ "athlete1" : {"name" : "bob", "somedynamicvalue" : "124548412" } }
seems relatively common can't seem find simple solution.....maybe i'm not sure how ask question correctly.
any appreciated.
ps - thought me returning entities in rest api spring answer never addressed questions, how add dynamic values response.
edit: per request here's simplified code
public modelandview getathletes(httpservletrequest req, httpservletresponse res) throws webserviceexception { res.setcontenttype("text/xml"); list<athletestat> athletestats = athletemanager.getathletestats(); map<string, object> model = new hashmap<string, object>(); model.put("athletestats", athletestats); modelandview modelandview = new modelandview("athletes.ftl", "model", model); modelandview.setcacheable(true); return modelandview; } public class athletestat{ private long id; private string name; private string hometown; private string resides; private date birthdate; //getters , setters } <?xml version="1.0" encoding="utf-8" standalone="yes" ?> <#escape x x?xml> <response status="ok"> <data> <athletes> <#list model.athletestats athletestat> <athlete name="${athletestat.athlete.name}" internalid="${athletestat.athlete.id?c}"> <abbrevname><#if athletestat.athlete.abbrevname?exists>${athletestat.athlete.abbrevname}</#if></abbrevname> <birthdate><#if athletestat.athlete.birthdate?exists>${athletestat.athlete.birthdate?string("yyyy-mm-dd't'hh:mm:ss")}</#if></birthdate> <hometown><#if athletestat.athlete.hometown?exists>${athletestat.athlete.hometown}</#if></hometown> <resides><#if athletestat.athlete.resides?exists>${athletestat.athlete.resides}</#if></resides> <age><#if athletestat.athlete.age?exists>${athletestat.athlete.age}</#if></age> </athlete> </#list> </athletes> </data> </response> </#escape>
you can put in model additional map map dynamic values each athlete.
like this: have
model.put("athletestats", athletestats);
add generation of dynamic value map , add model:
model.put("athletestats", athletestats); map<integer, string> dynamicvalues = new hashmap<integer, string>(); for(athletestat athletestat : athletestats) { string dynvalue = athletestat.tostring(); /* calculate dynamic value */ dynamicvalues.put(athletestat.getid(), dynvalue); } model.put("dynamicvalues", dynamicvalues);
now add line map it:
<dynamic> <#if model.dynamicvalues.get(athletestat.athlete.id)?exists> ${model.dynamicvalues.get(athletestat.athlete.id)} </#if> </dynamic>
like:
... <age><#if athletestat.athlete.age?exists>${athletestat.athlete.age}</#if></age> <dynamic><#if model.dynamicvalues.get(athletestat.athlete.id)?exists>${model.dynamicvalues.get(athletestat.athlete.id)}</#if></dynamic> </athlete> </#list>
Comments
Post a Comment