【问题标题】:Error while creating bean with name "defaultServletHandlerMapping"创建名称为“defaultServletHandlerMapping”的 bean 时出错
【发布时间】:2017-02-26 00:10:59
【问题描述】:

我正在尝试运行一个使用注解配置的 Spring Boot 应用程序,下面是 WebConfig.java 文件,

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.kumar.codebuzz"})
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new SecurityHandlerInterceptor()).addPathPatterns("/v1/app/*").excludePathPatterns("/v1/generateOTP", "/v1/validateOTP", "/users/signUp");
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/swaggerui/**")
            .addResourceLocations("classpath:/swaggerui/");
    registry.addResourceHandler("/webview/**")
            .addResourceLocations("classpath:/webview/");
}

@Bean
public ViewResolver configureViewResolver() {
    InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
    viewResolve.setPrefix("/WEB-INF/views/");
    viewResolve.setSuffix(".jsp");
    return viewResolve;
}


}

我无法启动应用程序,下面是堆栈跟踪,

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling

无法解决问题。

谢谢,

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    这将发生在以下情况

    1) 未提及 @ComponentScan - 这不适用于您的情况,因为您已在 WebMvcConfiguration 类中提及它

    2) @Configuration 在同一个项目的其他地方使用 - 检查您的项目并查找 @Configuration,如果您不小心将其删除。

    还有另一种去除方式

    将 WebMvcConfiguration 中的 @ComponentScan 替换为下面给出的条目

    @ComponentScan(basePackages = { "com.kumar.codebuzz" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.class) })
    

    参考资料:

    Error with Spring BOOT

    Error creating bean with name 'defaultServletHandlerMapping

    Error creating bean with name defaultServletHandlerMapping

    【讨论】:

    • 试了第二种方法,还是一样的错误。
    • 你有没有看过我提供的参考资料,这也可能对你有帮助。
    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 2017-06-05
    • 2015-02-23
    • 1970-01-01
    • 2022-07-21
    • 2020-02-07
    • 2018-12-17
    • 2020-07-24
    相关资源
    最近更新 更多