Restlet 有 SelfAttachingServerResource 吗?

Is there a SelfAttachingServerResource for Restlet?

我正在尝试在 Restlet 之上创建一个框架,我的问题是,是否可以从 org.restlet.Application 代码之外让 ServerResource 成为 "injected"?

标准方法是在此处注入资源:

public class FrameworkApplication extends Application {
    private static final String ROOT_URI = "/";
  /**
   * Creates a root Restlet that will receive all incoming calls.
   */
  @Override
  public Restlet createInboundRoot() {
    Router router = new Router(getContext());
    router.attach(ROOT_URI, RootServerResource.class);
    return router;
  }
}

然而,由于我正在构建框架,所以 FrameworkApplication 的使用是通过将其作为依赖项包括在内:

<dependency>
    <groupId>com.mycompany</groupId>
    <artifactId>myframework</artifactId>
    <version>0.0.1</version>
</dependency>

回到问题,我想知道是否可以像这样在Restlet路由中添加一个ServerResource:

public class PluggableResource extends ServerResource {
    @Get("json")
    public String represent() {
        // stuff
        return resp;
    }
}

在依赖注入的情况下,解决方案是,SelfInjectingServerResource现在我可以做这样的SelfAttachingServerResource吗?

我不知道您到底想做什么,但 Restlet 不支持服务器资源的自动发现支持。你需要在Restlet的classApplication的专用实现中自己实现。方法 createInboundRoot 将负责扫描 class 路径以检测服务器资源。此外,您需要为服务器资源添加更多元数据(例如带有注释)以指定附件路径。

但是,JAX-RS 支持提供了此功能。它提供了一组注释,以便于识别服务器资源并提供元数据,如附件路径、方法和方法的交换媒体类型。这是一个使用示例:http://restlet.com/technical-resources/restlet-framework/guide/2.3/extensions/jaxrs. The classes for server resources need to be register by hand but you can go further. As a matter of fact, you can scan the classpath to detect classes having the annotation Path. See this link for the way to implement such ferature: Scanning Java annotations at runtime。在这种情况下,它们将根据注释自动检测。有什么可以满足您的需求吗?

希望对您有所帮助。 蒂埃里