Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Restlet中的API模块化_Api_Restlet_Modularization - Fatal编程技术网

Restlet中的API模块化

Restlet中的API模块化,api,restlet,modularization,Api,Restlet,Modularization,我开发了一个基于restletapi的web应用程序。随着时间的推移,我添加了更多的功能,有时我需要在不同的端点下重用类似的RESTAPI组,这提供了稍微不同的执行上下文(比如使用相同的模式切换数据库的不同实例)。我喜欢重构代码,使API可重用,并在不同的端点重用它们。我最初的想法是为每个可重用API设计一个应用程序,并将它们连接到路由器上: router.attach("/context1",APIApplication.class) router.attach("/foo/context2"

我开发了一个基于restletapi的web应用程序。随着时间的推移,我添加了更多的功能,有时我需要在不同的端点下重用类似的RESTAPI组,这提供了稍微不同的执行上下文(比如使用相同的模式切换数据库的不同实例)。我喜欢重构代码,使API可重用,并在不同的端点重用它们。我最初的想法是为每个可重用API设计一个应用程序,并将它们连接到路由器上:

router.attach("/context1",APIApplication.class)
router.attach("/foo/context2",APIApplication.class)
该API应该与REST API的配置无关。将上下文信息(例如数据库实例)传递给应用程序API的最佳方式是什么?这种方法可行且正确吗?在Restlet中重用restapi的最佳实践是什么?请提供一些代码示例来说明您的答案


感谢您的帮助。

我看到了这个基本设置,它使用组件作为顶级对象运行,将子应用程序连接到虚拟主机,而不是路由器,如此框架示例所示

public class Component extends org.restlet.Component
{
  public Component() throws Exception
  {
    super();
    // Client protocols
    getClients().add(Protocol.HTTP);

    // Database connection
    final DataSource dataSource = InitialContext.doLookup("java:ds");
    final Configuration configuration = new Configuration(dataSource);

    final VirtualHost host = getDefaultHost();

    // Portal modules
    host.attach("/path1", new FirstApplication());
    host.attach("/path2", new SecondApplication(configuration));
    host.attach("/path3", new ThirdApplication());
    host.attachDefault(new DefaultApplication(configuration));
  }
}
我们使用自定义配置对象(基本上是pojo)在需要时传递任何公共配置信息,并使用它来构造应用程序,我们为每个应用程序使用单独的“默认”上下文

这最初是针对restlet 1.1.x编写的,并已通过2.0.x升级到2.1.x,尽管它可以正常工作并且相当整洁,但在2.1.x或2.2.x版本中可能有更好的方法来实现