rest - OSGi JAX-RS 和 bnd 声明式服务

标签 rest osgi jax-rs code-injection bnd

我想将我的 EE 应用程序迁移到 OSGi。我的应用程序由业务库、数据库 JPA/Entities 和一个 REST/WS 接口(interface)组成。它还有一个网络客户端。

我首先对结构进行原型(prototype)设计,并使所有接口(interface)和包以 OSGi 干净的方式相互通信。我想尽可能使用干净的规范,而不需要任何特定的供应商或框架。

我正在使用 bnd maven 插件来生成 list 和声明性服务。我想使用注入(inject)从我的其余资源调用 OSGI 服务(在另一个包上),如下所示:

@Path("some-resources")
@Component
public class SomeResources{

   private SomeService service = null;

   @Reference
   public void setController(SomeService service) {   // <- this is never called
    this.service = service;
   }

   @GET
   @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
   public Object getSomeService() {                  // <- called 
    try {
        service.process("Hello World");              // <- Error null object
   }
    ...

}

我可以用 bnd @Component 注释资源吗?并且可以@Resource被注入(inject)?
一切正常,但服务始终为空。

应该如何为 BND 声明我的捆绑包以使其成为 web/wab 包?

我使用 Maven 包:
<packaging>bundle</packaging>

...

        <plugin>                    
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version>
                <extensions>true</extensions>
                <dependencies>
                    <dependency>
                        <groupId>biz.aQute</groupId>
                        <artifactId>bndlib</artifactId>
                        <version>1.50.0</version>
                    </dependency>
                </dependencies> 
                <configuration>
                    <supportedProjectTypes>
                        <supportedProjectType>ejb</supportedProjectType>
                        <supportedProjectType>war</supportedProjectType>
                        <supportedProjectType>wab</supportedProjectType>
                        <supportedProjectType>bundle</supportedProjectType>
                        <supportedProjectType>jar</supportedProjectType>
                    </supportedProjectTypes>
                    <instructions>
                        <_include>-osgi.bundle</_include>
                    </instructions>
                </configuration>
                <executions>
                    <execution>
                        <id>bundle-manifest</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>manifest</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>bundle-install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>install</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>                    

 ...

带 bnd 指令
Web-ContextPath: my-root-http/rest/
Service-Component: *

最佳答案

OSGi 有一部分规范称为远程服务。简而言之,它的工作方式是您可以使用特殊的服务属性注册服务,并且基于属性技术应该选择您的服务并从中创建一个端点。它不仅与 REST 有关,还与任何处理远程调用的技术有关。您可以在“远程服务”一章下的 OSGi 核心规范中找到信息。

好吧,它是一个规范,但谁来实现它?目前我尝试了两个更大的项目。 CXF DOSGi 和 Eclipse ECF。他们提供了多种支持远程服务规范的技术。 CXF 特别支持 Jax-RS,基于它在服务器端和客户端的实现。

因为我不想在 OSGi 中使用 spring 特定的解决方案,所以我最终没有使用 CXF,而是创建了我自己的解决方案。它基于 Jersey 和远程服务规范。当使用 service.exported.interfaces=* 和 service.exported.configs=org.everit.osgi.remote.jersey 指定 OSGi 服务时,它将使用 HttpService 在/rest/路径下创建一个休息端点。您的捆绑包不必是 wab,它可以是简单的捆绑包。

我必须提到,如果您通过任何远程服务实现公开您的服务,您应该将 Jax-RS 注释带入由您的原始类实现的接口(interface),并基于该接口(interface)公开您的服务。

我建议您应该使用与 Spring 惊人地相似的 Blueprint(OSGi 规范的一部分),而不是 OSGi 中的 @Resource 和 @Component 注释。目前 Apache Aries 和 Gemini Blueprint 实现了它。使用蓝图,您可以轻松创建 bean 并将它们相互连接。如果以这种方式注册您的远程服务,您可以在蓝图的帮助下设置任何属性(就像 spring applicationcontext.xml 中 bean 的属性一样)。

您可以在 https://source.everit.biz/svn/everit-osgi/trunk/samples/jaxrs/ 找到我制作的示例应用程序。 (用户/密码:访客/访客)。 http://cookbook.everit.org 上有一个指南解释了如何开始和开发这些示例。

我希望示例应用程序可以帮助您开始使用远程服务规范一章。

要了解如何使用 JPA 和注入(inject)(蓝图),您应该查看 OSGi 纲要规范以了解可能性并找到您喜欢的实现。我还制作了一个基于 blueprint 和 hibernate-jpa 的示例项目,您可以将其作为我已经提供的示例 url 的兄弟找到。

更新

还有一个 JAXRS 扩展器实现,我在 https://github.com/everit-org/osgi-remote-jersey .有关文档,请参阅自述文件。它与第一个不同之处在于它基于白板服务属性的工作方式。

关于rest - OSGi JAX-RS 和 bnd 声明式服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10200347/

相关文章:

spring - OSGi 包读取配置属性

java - 将 Spring bean 注入(inject) Camel DSL

java - 忽略 Swagger 模型中的未知属性

java - servlet 和 JAX-RS 依赖项来自哪里?

java - J2EE 7 Rest 客户端中的属性而不是值

java - 使用 HttpsURLConnection 进行 GET 调用来获取图像

ruby - RestClient 使用 Resource 进行 GET 操作

java - 将文件夹添加到 RCP 产品中的类路径

node.js - 如何在服务器上创建流端点?

rest - 如何为 Azure 存储构建授权 header 获取容器属性 REST API