spring java.lang.IllegalArgumentException:需要 ServletContext 来配置默认的 servlet 处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21516683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:31:27  来源:igfitidea点击:

java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling

springspring-mvcspring-test

提问by balteo

I have the following test class:

我有以下测试类:

@ActiveProfiles({ "DataTC", "test" })
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {BaseTestConfiguration.class, DataTestConfiguration.class, JpaConfiguration.class, PropertyPlaceholderConfiguration.class })
public class RegularDayToTimeSlotsTest {
...

The issue seems to come from the BaseTestConfiguration class:

问题似乎来自 BaseTestConfiguration 类:

@Configuration
@ComponentScan(basePackages = { "com.bignibou" }, excludeFilters = { @Filter(type = FilterType.CUSTOM, value = RooRegexFilter.class),
        @Filter(type = FilterType.ANNOTATION, value = Controller.class), @Filter(type = FilterType.ANNOTATION, value = ControllerAdvice.class) })
public class BaseTestConfiguration {

}

I systematically get this exception:

我系统地得到了这个例外:

Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.<init>(DefaultServletHandlerConfigurer.java:54)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:329)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$bb4ceb44.CGLIB$defaultServletHandlerMapping(<generated>)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$bb4ceb44$$FastClassByCGLIB$8bb5c1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:326)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$bb4ceb44.defaultServletHandlerMapping(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    ... 43 more

I am not sure how to get around this issue. Somehow Spring is looking for a ServletContext when I run the test and I get the above exception...

我不知道如何解决这个问题。不知何故,当我运行测试时,Spring 正在寻找一个 ServletContext 并且我得到了上述异常......

回答by Sam Brannen

One of your @Configurationclasses is obviously annotated with @EnableWebMvc. That's how DelegatingWebMvcConfigurationends up in your stack trace, since it is importedby @EnableWebMvc.

你的一个@Configuration类显然用@EnableWebMvc. 这是如何DelegatingWebMvcConfiguration结束在你的堆栈跟踪,因为它是进口@EnableWebMvc

So although you thinkyou don't need a WebApplicationContext(and hence a ServletContext), you in fact do need it simply because you are loading an application context with @EnableWebMvc.

因此,尽管您认为不需要 a WebApplicationContext(因此不需要 a ServletContext),但实际上您确实需要它,因为您正在使用@EnableWebMvc.

You have two options:

您有两个选择:

  • Compose the configuration classes for your integration test so that you are not including the web-related configuration (i.e., the @Configurationclass(es) annotated with @EnableWebMvc).
  • Annotate your test class with @WebAppConfigurationas suggested in other comments above.
  • 为您的集成测试编写配置类,这样您就不会包含与 Web 相关的配置(即@Configuration用 注释的类@EnableWebMvc)。
  • @WebAppConfiguration按照上面其他评论中的建议对您的测试类进行注释。

Regards,

问候,

Sam (author of the Spring TestContext Framework)

Sam(Spring TestContext 框架的作者)

回答by Sotirios Delimanolis

It seems like you are missing

好像你失踪了

@WebAppConfiguration

from your test class.

从你的测试课。

The documentationstates

文件指出

The resource base path is used behind the scenes to create a MockServletContext which serves as the ServletContext for the test's WebApplicationContext.

资源基本路径在幕后用于创建 MockServletContext,该 MockServletContext 作为测试的 WebApplicationContext 的 ServletContext。

Typically a Servlet container would provide the ServletContext. Since you are in a testing environment, you need a fake. @WebAppConfigurationprovides that.

通常 Servlet 容器会提供ServletContext. 由于您处于测试环境中,因此您需要一个假的。@WebAppConfiguration规定。

回答by Hrishikesh

For you to instantiate the Servlet context, you would have to use the annotation.

为了实例化 Servlet 上下文,您必须使用注释。

@WebAppConfiguration

A class-level annotation that is used to declare that the ApplicationContext loaded for an integration test should be a WebApplicationContext. The mere presence of @WebAppConfiguration on a test class ensures that a WebApplicationContext will be loaded for the test, using the default value of "file:src/main/webapp" for the path to the root of the web application (i.e., the resource base path). The resource base path is used behind the scenes to create a MockServletContext which serves as the ServletContext for the test's WebApplicationContext.

用于声明为集成测试加载的 ApplicationContext 应该是 WebApplicationContext 的类级别注释。@WebAppConfiguration 在测试类上的存在确保为测试加载 WebApplicationContext,使用默认值“file:src/main/webapp”作为 Web 应用程序根目录的路径(即资源基本路径)。资源基本路径在幕后用于创建 MockServletContext,该 MockServletContext 作为测试的 WebApplicationContext 的 ServletContext。

回答by Robert Hunt

I was getting a similar error but whilst running the application normally rather than trying to run tests.

我遇到了类似的错误,但同时正常运行应用程序而不是尝试运行测试。

It turns out if you're making use of a custom PermissionEvaluatorthen you need to declare it in a separate @Configurationclass to the one with your main Spring security configuration in.

事实证明,如果您正在使用自定义,PermissionEvaluator那么您需要在一个单独的@Configuration类中将其声明为包含主要 Spring 安全配置的类。

See: How do I add method based security to a Spring Boot project?

请参阅:如何向 Spring Boot 项目添加基于方法的安全性?

There is also an open Github issue: https://github.com/spring-projects/spring-boot/issues/4875

还有一个开放的 Github issue:https: //github.com/spring-projects/spring-boot/issues/4875