我正在使用 Restlet 库,我想知道在通过 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 准则的最佳方式。
因此,除非您将名称和描述转换为资源,否则请保持这种方式。