java - Create a text/plain Jersey response -
i have code works, looking better way it. have restful web api want support json, xml, , text media types. json , xml easy jaxb annotated "bean" class. got text/plain work, wish jersey little more intelligent , able convert list of beans, list<machine>
string using tostring
.
here resource class. json , xml media types use jaxb annotated bean class. text plain uses custom string format (basically stdout representation of command).
@path("/machines") public class machineresource { private final machinemanager manager; @inject public machineresource(machinemanager manager) { this.manager = manager; } @get @path("details/") @produces({ mediatype.application_json, mediatype.application_xml }) public list<machine> details() { return manager.details(); } @get @path("details/") @produces({ mediatype.text_plain }) public string detailstext() { stringbuilder text = new stringbuilder(); for(machine machine : manager.details()) { text.append(machine.tostring()); } return text.tostring(); }
is there better way jersey automatically converting string, have implement 1 method here? (that can handle 3 media types)
i see can implement messagebodywriter, seems lot more trouble.
edit:
if matters, using embedded jetty , jersey options.
thanks!
implementing messagebodyreader
/writer
need in order following:
@get @path("details/") @produces({ mediatype.application_json, mediatype.application_xml, mediatype.text_plain }) public list<machine> details() { return manager.details(); }
it's not code write, , if able write generic enough able re-use out of it.
Comments
Post a Comment