【问题标题】:filenotfoundexception running jerseyTest - missing applicationContext.xml运行 jerseyTest 的 filenotfoundexception - 缺少 applicationContext.xml
【发布时间】:2018-01-17 09:26:15
【问题描述】:

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

我已扩展 JerseyTest,但出现上述错误。 Jersey 给出的示例 link 似乎没有帮助。

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);
}
}

【问题讨论】:

    标签: java spring jersey-test-framework


    【解决方案1】:

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

    创建对象:

        final ResourceConfig resourceConfig = new ResourceConfig(restResource);
    

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

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

    这样就可以让JerseyTest成功运行了。

    【讨论】:

    • 由于某种原因这对我不起作用。 Jersey 测试容器仍然抱怨找不到applicationContext.xml。到目前为止,我发现的唯一解决方案是提供 applicationContext.xml
    【解决方案2】:

    如果您有 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 容器支持,请查看 this post 中的示例

    【讨论】:

    • 这是什么? AnnotationConfigApplicationContext(YourSpringConfig.class)。我尝试了 application 和 jerseyconfig,但它们不起作用。
    • 不太清楚你的意思。
    • 我访问了第 2 项的链接。我遵循了他们在所学课程中的内容。我正在为你好世界做 .packages(com.service.rs) 。我在注入/活页夹上收到错误 NoClassDefFound
    • 我不知道。可能版本有问题。
    • 我在 pom 中的所有版本都是 2.26-b09
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 2019-06-24
    相关资源
    最近更新 更多