在OSGi中获取WebTarget的实例

时间:2015-12-06 18:44:58

标签: java maven osgi jersey-2.0

我正在OSGi上运行一些测试并尝试使用以下代码创建WebTarget的实例来测试OSGi中发布的端点:

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
...
...

private static final String port = "8080";
private static final String CONTEXT = "/someContext";
private static final URI baseUri = URI.create("http://localhost:" + port + CONTEXT);

@Test
public void sentence() {
    Client c = ClientBuilder.newClient();
    final WebTarget target = c.target(baseUri);

    Response response = target.path("/service").request().post(Entity.entity(new MockMessage("123", "sessionId123"), MediaType.APPLICATION_JSON_TYPE), Response.class);

    logger.info("JERSEY RESULT = " + response.toString());
    assertEquals(Response.ok().build(), response);
}

问题是当它运行时会抛出异常java.lang.ClassNotFoundException: Provider org.glassfish.jersey.internal.RuntimeDelegateImpl could not be instantiated: java.lang.IllegalStateException: No generator was provided and there is no default generator registered。它最糟糕,因为缺少的类(RuntimeDelegateImpl)位于jersey-common-2.x.jar包的内部包下。

那么,有没有人知道如何在OSGi下创建WebTarget的实例?

顺便说一句,我已经在pom文件中有这两个依赖项了:

<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-common</artifactId>
  <version>2.22.1</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>javax.ws.rs</groupId>
  <artifactId>javax.ws.rs-api</artifactId>
  <version>2.0.1</version>
  <scope>test</scope>
</dependency>

1 个答案:

答案 0 :(得分:0)

当我使用Arquillian在OSGi下运行测试时,我最终通过添加注释@RunAsClient来解决它,以在OSGi容器外运行测试,并避免在.internal包中找不到依赖项