Multiple get methods in single Resource class with Restlet

末鹿安然 提交于 2019-12-11 06:30:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!