球衣 2 + 灰熊 2 + Apache Felix + 德布里

Jersey 2 + Grizzly 2 + Apache Felix + Debry

您如何评价这个解决方案?

让所有这些在 Apache Felix OSGi 上运行,为您提供一个轻量级但功能强大的完整 OSGi 解决方案。

安装 Felix HTTP API 和 BASE 然后将 grizzly-httpservice-bundle-2.3.22.jar 复制到您的包目录中。

(如果您想先测试完整的 Web 控制台)

启动所有服务并确保您的 Web 控制台正常工作。

你所做的是:

将 Jersy 2 RI 目录中的所有库复制到你的 felix bundle 目录(那些被列为文件但没有真正描述的可以删除)并将 derby.jar 也复制到那个目录。

这是我的工作 Felix 包目录:

aopalliance-repackaged-2.4.0-b31.jar asm-debug-all-5.0.4.jar derby.jar grizzly-httpservice-bundle-2.3.22.jar hk2-api-2.4.0-b31.jar hk2-locator-2.4.0-b31.jar hk2-utils-2.4.0-b31.jar javassist-3.18.1-GA.jar javax.annotation-api-1.2.jar javax.inject-2.4.0-b31.jar javax.servlet-api-3.0.1.jar javax.ws.rs-api-2.0.1.jar jersey-client.jar jersey-common.jar jersey-container-servlet-core.jar jersey-container-servlet.jar jersey-guava-2.21.jar jersey-media-jaxb.jar jersey-server.jar org.apache.felix.gogo.command-0.14.0.jar org.apache.felix.gogo.runtime-0.16.2.jar org.apache.felix.gogo.shell-0.10.0.jar org.apache.felix.http.api-3.0.0.jar org.apache.felix.http.base-3.0.0.jar org.apache.felix.log-1.0.1.jar org.apache.felix.webconsole-4.2.10-all.jar osgi-resource-locator-1.0.1.jar validation-api-1.1.0.Final.jar

创建一个基本的 OSGi 包...

普通的激活器覆盖方法看起来像这样..(不包括错误检查:)使事情更清楚......但应该添加)

@Override
public void start(BundleContext context) throws Exception {

ServiceReference refHttpService = 
    context.getServiceReference(HttpService.class.getName());

HttpService httpService = (HttpService) context.getService(refHttpService);

ResourceConfig rc = new ResourceConfig(CHelloResource.class);

ServletContainer servletContainer = new ServletContainer(rc);

httpService.registerServlet(
    "/j",
    servletContainer,
    null,
    httpService.createDefaultHttpContext());
}



@Override
public void stop(BundleContext context) throws Exception {

ServiceReference refHttpService = 
    context.getServiceReference(HttpService.class.getName());

HttpService httpService = (HttpService) context.getService(refHttpService);

httpService.unregister("/j");
}

:)

分配比预期的更简单?加入 JAX-RS 和 OSGi ?

我在您的代码中发现的一个错误是您使用了 getService 但没有使用 ungetService。

除此之外,您可能希望使用框架,这样您就不必 fiddle 使用低级 OSGi api。您应该研究声明式服务和蓝图。

另一件事是,您提供休息的方法非常轻量级,但也非常有限。例如,您将如何进行日志记录或安全性。所以 Apache CXF 可能是一个不错的选择,因为它提供了很多附加功能。

手动搜索必要的包来安装也可能会很麻烦。为此,您可能需要查看 Apache Karaf,它提供了许多基础设施,例如 http 和 rest 支持,易于安装。它比纯 felix 轻一点,但更容易上手。 See some tutorials here.