rest - What is to prefer in Restlet: handleGet, handlePost OR represent, acceptRepresetation? -


imho, there 2 techiques handle query resource:

  • for http can override represent(variant variant) or handleget().
  • for http post same applies acceptrepresentation(representation entity) , handlepost().

the doc handleget says:

handles call automatically returning best representation available. content negotiation automatically supported based on client's preferences available in request. feature can turned off using "negotiatecontent" property.

and represent:

returns full representation given variant returned via getvariants() method. default implementation directly returns variant in case variants full representations. in other cases, need override method in order provide own implementation.

what main differences between these 2 types of implementations? in case should prefer 1 on other? right can achieve e.g. handleget() work represent()?

i first started using handleget setting entity response. when implemented project used represent. looking can't 1 way better or clearer other. expirences that?

i recommend using represent(variant) because you’ll leveraging content negotiation functionality provided default implementation of handleget(request, response).

btw, lately i've started using annotation-based syntax instead of overriding superclass methods, , it. find clearer, simpler, , more flexible.

for example:

@post('html') representation dosearch(form form) throws resourceexception {     // field form     string query = form.getfirstvalue("query");      // validate form - primitive example of course     if (query == null || query.trim().length() == 0)         throw new resourceexception(status.client_error_bad_request, "query required.");      //     searchresults searchresults = searchengine.dosearch(query);      // return html representation     return new stringrepresentation(searchresults.ashtmlstring(), mediatype.text_html); } 

the advantages of using approach include incoming representation being automatically converted useful form, method can named whatever makes sense application, , scanning class can see class methods handle http methods, kind of representations.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -