How to filter the json response returning from spring rest web service -
how filter json response returning spring rest web service.
when use call customevents need outout eventid , event name only. when ask specifc event need send full details of event.
class customevent{ long id; string eventname; account createdby; account modifiedby; .. } class account{ long id; string fname; string lname; .... } @controller public class customeventservice { @requestmapping("/customevents") public @responsebody list<customevent> getcustomeventsummaries() {} @requestmapping("/customevents/{eventid}") public @responsebody customevent getcustomevent(@pathvariable("eventid") long eventid) {} }
how can achieve above? i'm using spring 3.1 @ moment. ther support in 3.1 version achieve above or later verion
i can think of 2 solutions off hand both take advantage of jackson's mixin feature.
the first solution far more complicated awesome approach if describing replicated in other parts of code described in this link. happens define aspect applies specific mixin (in case customeventmixin) have set in jsonfilter annotation.
the second solution far simpler , includes using jackson object mapper on own (instead of delegating responsibility string) following code
@controller public class eventcontroller { private objectmapper objectmapper = new objectmapper(); public eventcontroller(objectmapper objectmapper) { this.objectmapper = objectmapper; objectmapper.addmixinannotations(customevent.class, customeventmixin.class); } @requestmapping("/customevents") @responsebody public string suggest() { return objectmapper.writevalueasstring(getcustomevents(), new typereference<list<customevent>>() {}); } }
in both cases need define customeventmixin according jackson rules
update:
an example mixin class (say want ignore id)
public interface customeventmixin { string name; @jsonignore string id; }
Comments
Post a Comment