SLA
SLA

Reputation: 23

Restlet: GET requests using queries that access more than one resource

In a database, lets say I have the name and date of birth of many persons and I want to GET the name of persons that are older than 20 years. I might use something like this :

http://www.example.com/people?age=over20

In this case the router will attach this uri to the people resource class. Are there any functions that could parse the uri and get the needed request (in this example, age>20) ? or should I write my own functions ?

Another issue, in this example, the query ( age>20 ) is simple, so it could be included in the uri, what if I have complex queries (union between two tables ..etc ) ? should I put the query in xml file and send it to the server?

Upvotes: 1

Views: 2149

Answers (1)

Charlie
Charlie

Reputation: 7349

Are there any functions that could parse the uri and get the needed request (in this example, age>20) ?

Yes there are functions to parse the query string. In this particular case, using Restlet, you could get the value in your resource through: getRequest().getResourceRef().getQueryAsForm().getFirst("age").getValue() == "over20"

There could be other ways as well (and maybe it's improved in Restlet 2.0), but it's been my experience that Restlet, at least was, very structured and verbose. I've since moved to JAX-RS and Jersey to make my resources since it lets me accomplish a lot of the same with only a few annotations. (in this case @QueryParam("age").) You may consider making the over/under part of the key instead of the value, so that you can more easily parse the string and build your query. The idea being it'd be the difference between trying to handle: http://www.example.com/people?age=over20&age=under30

versus handling: http://www.example.com/people?ageAtLeast=20&ageAtMost=30

Another issue, in this example, the query ( age>20 ) is simple, so it could be included in the uri, what if I have complex queries (union between two tables ..etc ) ?

Here I would recommend looking for discussions on Restful URI design. Ideally different entities would be different uri paths, and their relations would be apparent through your URL. For example there was a highly voted question: RESTful URL design for search

should I put the query in xml file and send it to the server?

Technically this is a potential option, but I would avoid it if at all possible. It's very obtuse, especially for simple GETs. (Also note, that while technically not forbidden, an entity body on a GET is not standard, and there are many products that do not handle it properly, so you'd wind up abusing POST or another method to accomplish this).

Upvotes: 4

Related Questions