WebMvcConfigurer 在 Spring Boot 中不起作用?

杰特·罗伊

我改变了我完美工作的控制器类,它只是为了实现视图分辨率,如下所示:

@Controller
public class MyController {

@GetMapping("/signup")
public String signupPage() {
    return "signup";
}

@GetMapping("/login")
public String loginPage() {
    return "login";
}

@GetMapping("/dashboard")
public String dashboardPage() {
    return "dashboard";
}

@GetMapping("/logout")
public String logoutPage() {
    return "redirect:/";
}
}

对于扩展 WebMvcConfigurer 的类,它具有如下所示的所有视图解析器:

@Configuration
@EnableWebMvc
public class ViewConfig implements WebMvcConfigurer {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/signup").setViewName("signup");
    registry.addViewController("/login").setViewName("login");
    registry.addViewController("/dashboard").setViewName("dashboard");
    registry.addViewController("/logout").setViewName("redirect:/");
}

这感觉更加简洁,干净。

但是,每当我尝试加载这些页面中的任何一个时,这都会给我 405 Method Not Allowed 错误。为什么会这样?spring boot 不支持 WebMvcConfigurer 吗?

编码器

这在Spring Boot Documentation 中提到,在 spring mvc 部分下你可以使用 WebMvcConfigurer,但你不需要做 @EnableWebMvc

所以你应该删除@EnableWebMvc 注释!

@Configuration
// @EnableWebMvc Remove this!
public class ViewConfig implements WebMvcConfigurer {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/signup").setViewName("signup");
    registry.addViewController("/login").setViewName("login");
    registry.addViewController("/dashboard").setViewName("dashboard");
    registry.addViewController("/logout").setViewName("redirect:/");
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Boot DevTools在Eclipse中不起作用

@Transactional在带有CrudRepository的Spring Boot中不起作用

多模块组件扫描在Spring Boot中不起作用

事务注释在Spring Boot中不起作用

为什么RestController在Spring Boot中不起作用?

我的BeanPostProcessor在Spring Boot中不起作用

部署的Spring-Boot战争在Tomcat中不起作用

实时重载在Spring Boot devtools中不起作用

Spring Boot中的异常处理程序不起作用?

Spring Boot方面在我的API中不起作用

json中的Json在Spring Boot中不起作用

全局CORS配置在Spring Boot中不起作用

将Spring Boot WebMvcConfigurer限制为仅指定路径

Spring Boot Kotlin项目在Eclipse中不起作用

RestController在oauth2 Spring Boot中不起作用

Spring Boot-@PreAuthorize在测试中不起作用

Primefaces FileUpload在Spring Boot中不起作用

Spring Boot Autowired在配置类中不起作用

CommonsRequestLoggingFilter在Spring Boot应用程序中不起作用

注释CrossOrigin在Spring Boot中不起作用

无法在Spring Boot 2应用程序中为添加拦截器配置WebMvcConfigurer

自定义MessageConverter在Spring Boot中不起作用

Spring Boot permitAll在WebSecurityConfigurerAdapter中不起作用

Spring Boot 2.1:在 WebMvcConfigurer#addFormatters(...) 中抛出的异常未在 @RestControllerAdvice 中捕获

Spring Boot 2.2.2 - Prometheus 在 Actuator 中不起作用

spring boot中的BindingResult参数不起作用

Spring Boot @Scheduler 在 Google Kubernetes Engine 中不起作用

@ConstructorBinding 在 Spring Boot 2.5.4 中似乎不起作用

Spring Boot - @Autowired 在 @Service 中不起作用