使用 Restlet 在单个资源类中使用多个 get 方法
这是我的代码:
这是我的应用程序类>>>>>
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;
}
}
这是资源类>>>
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;
}
}
/你好
& /isuserloggedin
映射到相同的资源类,但是 我想要的是:当有 /hello
时,应该调用 userLogin
方法 当存在 /isuserloggedin
时,必须调用 isUserLoggedIn
。 这可能吗? 或者我错了? 如果这是不可能的那么任何人都可以告诉我任何其他选择吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Restlet 2.1(尝试 M7 或更高版本)中,可以将两个 HTTP GET 调用分派到同一资源类中的两个 Java 方法。这是通过利用如下查询参数来完成的:
但是,正如已经指出的,您最好使用单独的资源类。
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:
However, as pointed already, you would be better off using a separate resource class.
自从我上次使用 Restlet 以来已经有一段时间了,无论如何,如果您正在实现一个完整的 REST API,如果确实需要的话,我希望这两个资源能够成为单独的资源。否则,资源应该精确地映射到一种表示,恕我直言。
将两个 URI 映射到一个资源类(而不是分别实现 GET 方法的两个 URI)有什么好处?这似乎增加了一点歧义,但没有任何好处。
我将在用户表示中返回状态(已记录或未记录)信息。
现在,为了完整起见,我发现理解你的 API 语义有点困难:我可以询问任何用户的登录状态,还是只是我的?虽然不是很清楚,但我知道这不是重点。
作为一本不错的读物,您可能想看看一些流行的 REST api,看看它们如何管理与您类似的问题,即。我特别喜欢 Github Users' API。
希望有帮助。
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.