0

I am trying to run a spring boot application which uses annotation configuration, below is the WebConfig.java file,

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


}

i am not able to start the application, below is the stack trace,

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

not able to figure out the issue.

Thanks,

kumarD
  • 574
  • 2
  • 9
  • 23

1 Answers1

0

This would happen on below cases

1) @ComponentScan is not mentioned - This is not applicable in your case, since you have mentioned it in the WebMvcConfiguration class

2) @Configuration is used some where else in the same project - Check your project and find for @Configuration, if you have used it accidentally remove it.

There is also another way of removing it

Replace the @ComponentScan in your WebMvcConfiguration with the entry given below

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

References:

Error with Spring BOOT

Error creating bean with name 'defaultServletHandlerMapping

Error creating bean with name defaultServletHandlerMapping

Community
  • 1
  • 1
Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34