【问题标题】:Resource not exported up in OSGi container资源未在 OSGi 容器中导出
【发布时间】:2018-08-12 23:28:59
【问题描述】:

我正在尝试通过 OSGi(使用 Apache Felix)公开 REST 服务。我正在使用osgi-jax-rs-connector 发布资源。这里是资源接口:

@Path("/bingo")
public interface BingoService {

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/lottery")
List<Integer> getLottery();
}

该实现使用 DS 注释来获取对容器中提供的服务的引用:

@Component(
    immediate = true,
    service = BingoService.class,
    properties = "jersey.properties")
public class Bingo implements BingoService {

@Reference
private RandomNumberGenerator rng;

@Activate
public void activateBingo() {
    System.out.println("Bingo Service activated using " +
                        rng.getClass().getSimpleName());
}

@Override
public List<Integer> getLottery() {
    List<Integer> result = new ArrayList<>();
    for (int i = 5; i > 0; i--) {
        result.add(rng.nextInt());
        }
    return result;
    }
}

jersey.properties 只包含这一行

service.exported.interfaces=*

当我部署捆绑包时,它会启动并正确注册服务。但是如果我去 http://localhost:8181/services/bingo/lottery 我会得到 404。 有人可以指出我的问题或给我一些关于在哪里寻找的建议吗?

【问题讨论】:

    标签: rest jax-rs osgi apache-felix declarative-services


    【解决方案1】:

    在读取 OSGi - JAX-RS 连接器的 documentation 时,它希望在服务实例对象上找到注释 @Path@Provider。您已将它们放置在组件实现的接口上。

    我不确定BingoService 接口的用途是什么。这对于 JAX-RS 服务不是必需的。通常你会使用它自己的类型(例如service=Bingo.class)或简单的java.lang.Object注册资源类。

    【讨论】:

    • 不幸的是,将注释直接放在服务上也不起作用。注意:我很确定在构造类时在运行时存在注释,因此将它们放在接口上实际上是相同的。
    • 注解不是继承的,所以放在界面上肯定是一样的。然而,一些框架在寻找它们时可能会选择遍历继承层次结构。您说 JAX-RS 注释具有运行时保留是正确的……没有这个,JAX-RS 根本无法工作!
    • 感谢您指定这一点。我从 Jersey 改为 cxf,它开始工作了。无论如何,知道为什么它不能与以前的解决方案一起使用会很有趣。
    猜你喜欢
    • 2016-10-23
    • 2011-11-25
    • 2017-05-04
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    相关资源
    最近更新 更多