我正在使用Restlet library我想知道通过URL访问时是否可以调用类的特定方法。
现在我有这样的事情:
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/monitor", Monitor.class);
}
通过 URL 访问 /monitor/
时会调用 Monitor
类。
我希望能够做这样的事情:
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/monitor/name", Monitor.getName());
router.attach("/monitor/description", Monitor.getDescription());
}
这可以通过 ReSTLet 框架实现吗?
现在我发现的解决方法是使用 GET 参数并在 represent
方法上使用条件:
public StringRepresentation represent() {
String type= getQuery().getValues("type");
if(type.equals("getName")){
this.getName();
}
if(type.equals("getDescription")){
this.getDescription();
}
}
但这听起来不像是这样做的方法。
最佳答案
您的解决方案是验证 REST 准则的最佳方式。
因此,除非您将名称和描述转换为资源,否则就这样。
关于java - 使用ReSTLet框架调用特定方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22144644/