【问题标题】:Basic JAX-RS servlet in OSGi 4.2 environmentOSGi 4.2 环境中的基本 JAX-RS servlet
【发布时间】:2013-04-07 20:04:34
【问题描述】:

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

虽然在我看来,用HttpService 注册一个 Jersey 容器 servlet 应该可以解决问题,但我无法拼凑出一个仅包含几个包的集合。

这就是我的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);
}

【问题讨论】:

    标签: jersey osgi jax-rs


    【解决方案1】:

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

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

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

    这里是基于上述解决方案的最简单的代码 sn-p(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

    【讨论】:

    • svn 链接失效了。
    • @Jmini SVN 链接工作,但要求用户/通行证。使用用户通行证更新了描述,并且远程球衣在 github 上可用,因此也添加了该链接。请注意,由于答案还有许多其他解决方案,即使在 OSGi 纲要中也有关于如何提供 REST 服务的规范。
    【解决方案2】:

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

    请参阅 http://amdatu.org/components/web.html 了解说明和 tutorial video 如何创建基本 REST 资源。

    【讨论】:

    • 链接坏了
    【解决方案3】:

    这应该可以解决问题:https://github.com/hstaudacher/osgi-jax-rs-connector

    使用此项目中的发布者,您将能够将您的 OSGi 服务注册为 RESTful Web 服务,只需将它们注册为 OSGi 服务;)

    【讨论】:

      【解决方案4】:

      帮助我的是以下文章:Setting Up JAX-RS for Equinox OSGI

      重点是:

      • 球衣资源配置(@Path@Provider 带注释的类)需要使用org.glassfish.jersey.server.ResourceConfig 手动注册。
      • 所有其他配置选项(使用 javax.ws.rs.Applicationjersey.config.server.provider.packages 的 jersey 自动发现)将失败,因为 Jersey 服务器将无法在 Equinox 中扫描它们。
      • 在资源配置所在的包中实例化ServletContainer

      【讨论】:

        猜你喜欢
        • 2018-06-25
        • 1970-01-01
        • 2015-01-25
        • 2016-01-22
        • 2016-12-02
        • 1970-01-01
        • 2011-01-05
        • 2017-10-08
        • 1970-01-01
        相关资源
        最近更新 更多