问题
here is my code :
this is my application class >>>
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;
import firstSteps.UserResource;
public class FirstStepsApplication extends Application {
@Override
public synchronized Restlet createRoot() {
Router router = new Router(getContext());
router.attach("/hello", UserResource.class);
router.attach("/isuserloggedin",UserResource.class);
return router;
}
}
this is resource class >>>
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
/**
* Resource which has only one representation.
*
*/
public class UserResource extends ServerResource {
@Get
public String userLogin() {
return "This is userLogin method";
}
@Get
public boolean isUserLoggedIn(){
return false;
}
}
/hello
& /isuserloggedin
are mapped to same to resource class but
what i want is : when there is /hello
then userLogin
method should be called
and when there is /isuserloggedin
then isUserLoggedIn
must be called .
is this possible ??
or am i going wrong?
if this is not possible then any one can tell me any other alternative ?
回答1:
In Restlet 2.1 (try M7 or above), it is possible to dispatch two HTTP GET calls to two Java methods in the same resource class. This is done by leveraging query parameters like this:
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
/**
* Resource which has only one representation.
*
*/
public class UserResource extends ServerResource {
@Get
public String userLogin() {
return "This is userLogin method";
}
@Get("?loggedIn")
public boolean isUserLoggedIn(){
return false;
}
}
However, as pointed already, you would be better off using a separate resource class.
回答2:
Some time has passed since my last experience with Restlet, anyway, if you are implementing a fully REST API, I would expect the two to be separate resources if they really need to be. Otherwise, a resource should be mapped to exactly one representation, IMHO.
What is the benefit of having two URIs mapped to one resource class, instead of having two, each implementing the GET method? It seems to add a bit of ambiguity there, with no benefit.
I would return the status (logged or not) information in the user representation.
Now, for sake of completeness, I find a bit difficult to understand your API semantics: can I ask for the logged status of any user, or just mine? It's not very clear, but I understand that it was not the main point.
Just as a nice reading, you may want to have a look at some popular REST api to see how they manage issues similar to yours, ie. I like Github Users' API particularly.
Hope it helps.
来源:https://stackoverflow.com/questions/7608694/multiple-get-methods-in-single-resource-class-with-restlet