Reputation: 101
I changed my perfectly working controller class which were just doing the purpose of view resolution like so:
@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:/";
}
}
To a class extending WebMvcConfigurer, which had all the view resolvers like so:
@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:/");
}
This felt a lot more concise, and clean.
But this is giving me 405 Method Not Allowed error, whenever I try to load any of these pages. Why is this happening? Does spring boot not support WebMvcConfigurer?
Upvotes: 6
Views: 14907
Reputation: 354
This is mentioned in the Spring Boot Documentation, under the spring mvc section you can use WebMvcConfigurer, but you do not need to do @EnableWebMvc
So you should remove the @EnableWebMvc annotation!
@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:/");
}
Upvotes: 4