1

我正在对 Hello World 进行测试,试图在我的 spring java 程序中遵循 Jersey 框架。

我已经扩展了 JerseyTest,但我收到了上面的错误。泽西岛提供链接的示例似乎没有帮助。

public class SimpleTest extends JerseyTest {



@Override
protected Application configure() {
    return new ResourceConfig(RestService.class);
}

@Test
public void test() {
    final String hello = target("\service\hello").request().get(HelloModel.class);
    assertEquals("Hello World!", hello);
}
}
4

2 回答 2

2

在 Jersey 2.26 中,依赖项已更改为,但如果您不想启动整个 Spring 上下文,则需要jersey-spring4更多地清除答案。

创建对象:

    final ResourceConfig resourceConfig = new ResourceConfig(restResource);

就我而言,我只需要一个空的上下文:

    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.refresh();
    resourceConfig.property("contextConfig", context);

这能够让JerseyTest成功运行。

于 2018-03-12T02:49:43.067 回答
0

如果您有 jersey-spring3 依赖项,就会发生这种情况。默认行为是查找此 applicationContext.xml 文件(这是您的 Spring 上下文配置)。

如果你想为测试配置 Spring,你可以做几件事。

  1. 您可以手动创建ApplicationContext并将其传递给 Jersey

    ApplicationContext context = ...
    return new ResourceConfig(RestService.class)
        .property("contextConfig", context)
    

    如果您使用的是 xml 配置,那么您将创建一个ClassPathXmlApplicationContext. 如果您使用的是 Java 配置,那么您将创建一个AnnotationConfigApplicationContext.

  2. 如果您需要 servlet servlet 容器支持,请查看本文中的示例

于 2017-08-10T09:26:54.477 回答