在配置中,存在有关DispatcherServletPath的错误

时间:2018-09-20 03:17:18

标签: spring spring-boot configuration

错误输出,应用程序启动时发生错误,在spring-boot-starter-parent 2.0.5.RELEASE版本中,在1.5版本中,它可以正常工作。在版本2.0.5中,SpringBootServletInitializer文件的分发已更改;

Dependency annotations: {}
19:03:05.312 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of constructor in 
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.
- Bean method 'dispatcherServletRegistration' not loaded because DispatcherServlet Registration found servlet registration bean dispatcherServletRegistration


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' in your configuration.

配置:

@SpringBootApplication
@Import({
    CityContextConfig.class,
    CityPersistenceJpaConfig.class,
    CityServiceConfig.class,
    CityWebConfig.class
})
public class CityApp extends SpringBootServletInitializer {

@Bean
public DispatcherServlet dispatcherServlet() {
    return new DispatcherServlet();
}

@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
    final ServletRegistrationBean registration = new ServletRegistrationBean<>(dispatcherServlet(), "/api/*");

    final Map<String, String> params = new HashMap<String, String>();
    params.put("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
    params.put("contextConfigLocation", "org.spring.sec2.spring");
    params.put("dispatchOptionsRequest", "true");
    registration.setInitParameters(params);

    registration.setLoadOnStartup(1);
    return registration;
}

//

@Override
protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
    return application.initializers(new MyApplicationContextInitializer()).sources(CityApp.class);
}

public static void main(final String... args) {
    new SpringApplicationBuilder(CityApp.class).initializers(new MyApplicationContextInitializer()).listeners().run(args);
}

}

项目结构分为common和webapp两个模块

введите сюда описание изображения

2 个答案:

答案 0 :(得分:0)

Servlet上下文路径在2.x中已更改

这是我推荐的建议

  1. applicataion.properties中定义servlet上下文路径。

    server.servlet.path=/ # Path of the main dispatcher servlet.
    
  2. 确保您在这些配置类@Configuration上具有CityContextConfig.class, CityPersistenceJpaConfig.class, CityServiceConfig.class, CityWebConfig.class表示法

    @SpringBootApplication
    public class CityApp {
    
    
    @Override
    protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.initializers(new MyApplicationContextInitializer()).sources(CityApp.class);
    }
    
       public static void main(final String... args) {
          new SpringApplicationBuilder(CityApp.class).initializers(new MyApplicationContextInitializer()).listeners().run(args);
       }
    
    }
    

答案 1 :(得分:0)

我也在寻找答案。从Spring 1.5.x到2.0都有迁移指南。这里Migration Guide

我认为我们的解决方案就是这样。但是我不知道如何执行他们的建议。