2

在 OSGi 4 容器中创建基本 JAX-RS 环境的最简单方法是什么,而无需求助于 Apache CXF 等重量级解决方案。

尽管在我看来,使用 注册 Jersey 容器 servletHttpService应该可以解决问题,但我还不能拼凑出一个集合,其中只有几个包可以做到这一点。

这就是我的bundle Activator的精髓所在,Resources类实现了Application接口来解析哪些类有JAX-RS注解:

public void start(BundleContext context) throws Exception {
    // Find the HttpService
    ServiceReference ref = context.getServiceReference(HttpService.class.getName());
    HttpService service = (HttpService) context.getService(ref);    

    // Register a Jersey container
    ServletContainer servlet = new ServletContainer(new Resources());
    service.registerServlet("/services", servlet, null, null);
}
4

4 回答 4

4

这是一个适用于 Jersey 的示例应用程序:https ://source.everit.biz/svn/everit-osgi/trunk/samples/jaxrs/

运行 mvn install 后,您将在 target/eosgi-testing-dist/... 处看到带有 Jetty 的可运行 equinox 容器。您可以在 lib 文件夹中找到使用了哪些包。

它使用这个解决方案:https ://source.everit.biz/svn/everit-osgi/trunk/remote/jersey/

这里是基于上述解决方案的最简单的代码片段(myObj 是包含注释的对象):

javax.ws.rs.core.Application application = new DefaultResourceConfig();
application.getSingletons().add(myObj);

com.sun.jersey.spi.container.servlet.ServletContainer servletContainer =
       new ServletContainer(application);

Hashtable<String, String> initParams = new Hashtable<String, String>();
initParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
httpService.registerServlet("/rest", servletContainer, initParams, null);

请注意,如果您想拥有 JSON 转换等功能,“initparams.put”很有用。

更新

SVN 链接的用户/通行证:访客/访客

remote-jersey 项目不再维护,但在 github 上仍然可用:https ://github.com/everit-org/osgi-remote-jersey

于 2013-04-07T22:42:47.083 回答
3

Amdatu 提供了一些开源 RESTful 服务包,包括基于 Apache Wink 的 JAX-RS 解决方案。

See http://amdatu.org/components/web.html for a description and a tutorial video how to create a basic REST resource.

于 2013-04-09T18:57:00.940 回答
1

This should do the trick: https://github.com/hstaudacher/osgi-jax-rs-connector

Using the publisher from this project you will be able to register your OSGi service as RESTful web services by simply registering them as OSGi services ;)

于 2013-07-05T07:32:21.160 回答
0

What helped me is following article: Setting Up JAX-RS for Equinox OSGI

The key points are:

  • The jersey resource configuration (@Path, @Provider anotated classes) needs to be registered manually with a org.glassfish.jersey.server.ResourceConfig.
  • All other configuration options (jersey auto discovery with javax.ws.rs.Application or jersey.config.server.provider.packages) will fail because Jersey server will not be able to scan them in Equinox.
  • Instanciate the ServletContainer in the bundle where the resource configuration are located.
于 2018-02-15T07:55:25.297 回答