问题描述
这是我的代码:
这是我的应用类 >>>
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;
}
}
这是资源类>>>
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
映射到相同的资源类,但我想要的是:当有 /hello
然后 userLogin
方法应该被调用当有 /isuserloggedin
时,必须调用 isUserLoggedIn
.这可能吗 ??还是我错了?如果这是不可能的,那么任何人都可以告诉我任何其他选择吗?
/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 ?
推荐答案
在 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:
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.
这篇关于具有 Restlet 的单个资源类中的多个获取方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!