在 OSGi 中获取 WebTarget 的实例
Get Instance of WebTarget in OSGi
我正在 运行 一些 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
。最糟糕的是因为丢失的 class (RuntimeDelegateImpl
) 在 jersey-common-2.x.jar
包的 internal 包下。
那么,有人知道如何在 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>
由于我使用Arquillian来运行 OSGi下的测试,所以我最终通过在OSGi容器外的运行测试中添加注释@RunAsClient
来解决它并避免依赖在 .internal
包中找不到。
我正在 运行 一些 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
。最糟糕的是因为丢失的 class (RuntimeDelegateImpl
) 在 jersey-common-2.x.jar
包的 internal 包下。
那么,有人知道如何在 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>
由于我使用Arquillian来运行 OSGi下的测试,所以我最终通过在OSGi容器外的运行测试中添加注释@RunAsClient
来解决它并避免依赖在 .internal
包中找不到。