public class MyWebConfig implements WebMvcConfigurer { public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/orange").setViewName("success"); } }
为什么是实现WebMvcConfigurer接口而不是实现这个类WebMvcConfigurerAdapter ,通过查看WebMvcAutoConfiguration spring boot的 spring mvc的自动配置类发现 如果实现这个类会导致自动配置失效
type = Type.SERVLET) (Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class }) ({ WebMvcConfigurationSupport.class) (Ordered.HIGHEST_PRECEDENCE + 10) (DispatcherServletAutoConfiguration.class, ({ ValidationAutoConfiguration.class }) public class WebMvcAutoConfiguration {