Hello Laurent,
the router class which is in charge to dispatch a call between the
declared routes relies on a parameter called "routingMode". It can take
on of these values: best, first, last, custom, next and random.
By default, its routing mode is "best". Each declared route is defined a
score, according to the path of the incoming request, and the length of
the recognized characters.
In your case, both "/users/{id}" and "/users/identification-keys" give
the same score because they catch the same number of cracters, thus the
first declared route giving the best score is chosen.
So I'm enclined to say that the declaration order is not neutral using
the default routing mode, and thus it is preferable to declared the
routes without pattern before the ones using patterns (such as /users/{id}).
We may enhance the "best" algorithm by promoting the routes having the
best score but without a pattern. This question is open.
Regarding the 405 response, I suppose the "UserResource.class " does not
"implement the GET method" ie does not use the @Get annotation nor
implement the ServerResource#get(Variant) method, nor the
ServerResource#get() method . Could you tell us more about this point?
>Another question, should I attach route using a trailing slash in URI, or not,
>or should I declare two routes ?
I think that attaching "/users" works for both cases (requesting "/users" and
"/users/"), so to my taste, it is useless to declare the "/users/" route.
Some purists can consider that "/users/" expresses that this resource is a kind
of directory, a container of other resources.
best regards,
Thierry Boileau
> Hello,
> I have a question about route and router.
> I have the following router
> final Router router = new Router(getContext());
> router.attach("/users", UsersResource.class);
> router.attach("/users/{id}", UserResource.class);
> router.attach("/users/identification-keys",
> IdentificationKeysResource.class);
>
> return router;
>
> If I go to http://localhost/users/identification-keys I got a 405 error
> in return
> If I declare the last line first, like this
>
> final Router router = new Router(getContext());
> router.attach("/users/identification-keys",
> IdentificationKeysResource.class);
> router.attach("/users", UsersResource.class);
> router.attach("/users/{id}", UserResource.class);
> return router;
>
> then it works.
> I tried to change behavior of router using
> router.setDefaultMatchingMode(Template.MODE_EQUALS);
> but nothing seems to changed.
> Is it possibly something not correct in my resource class ?
> Right now, it's ok for me to declare in the second way my resource, but
> I think it should work also in the first way,
> as shown by the tutorial
> http://www.restlet.org/documentation/2.0/tutorial#part12
>
> Another question, should I attach route using a trailing slash in URI,
> or not, or should I declare two routes ?
> router.attach("/users", UsersResource.class);
> router.attach("/users/", UsersResource.class);
> Regards,
> Laurent.
>
> P.S: using restlet 2.0 M4
>
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2382106