创建名称为#34的bean时出错; defaultServletHandlerMapping"

时间:2016-10-17 05:33:32

标签: java spring spring-boot

我正在尝试运行使用注释配置的spring启动应用程序,下面是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

无法弄清楚问题。

谢谢,

1 个答案:

答案 0 :(得分:0)

这将发生在以下情况

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