Hi Thierry,

Thierry Boileau wrote:
on one hand, you define a "route" which binds all uris such as
"/users/{user}/birthday" to a new instance of the BirthdayResource
class.
On the other hand, the resource is in charge to perform the request,
that is to say return a representation (if any) in case of "GET"
requests, delete the resource in case of "DELETE" request, etc. The
resource knows what happens and what to do.

I see. So I guess it would be fair to consider that the Resource object is more or less the equivalent of a closure representing the dispatch of a request to the resource.


In your case, the birthday resource is responsible to get the user (if
any), and return a representation of the user's birthday only if the
user exists. For example:
  1- the BirthdayResource constructor looks for the user and store it
as an attribute
  2- the "getRepresentation(Variant)" method returns a representation
only if the user attribute is not null

I think I found my mistake. In Resource.handleGet(), it's the fact there isn't any Variant that leads to a CLIENT_ERROR_NOT_FOUND response status. However, I was using this in the constructor, whether-or-not the resource existed:
  getVariants().add(new Variant(MediaType.TEXT_PLAIN));
Therefore, I shouldn't add any variant before I've made sure the resource exists.

If a transactional database is used in the back-end, I suppose that adding the Variants ought to be done as part of the transaction that reads or write data corresponding to the Resource. Such a transaction could be in the constructor of the Resource.


I hope this will help you.
Yes, cheers.

Do you expose a lot of attributes such as "birthday"?

Not really at the moment, that was just an example. As you can guess from my questions, I'm still learning about the framework. I have a REST-based model in mind for my application that I would have implemented with servlets, by I thought I'd be good to investigate restlets. So far, so good, but I'm not sure I'll use the Resource part of the framework. I'm indeed planning to make a lot of short requests and I'm a bit concerned about creating a new instance of a Resource for every request in terms of performance. I guess using a Restlet directly might be more efficient in some cases.


Best regards,

Bruno.

Reply via email to