Custom exception handler for NoHandlerFoundException is not working without @EnableWebMvc

前端 未结 3 1239
猫巷女王i
猫巷女王i 2021-01-13 01:00

I want to override html error page for 404 responses as an JSON response. When i use @ControllerAdvice without @EnableWebMvc it is not working.

3条回答
  •  伪装坚强ぢ
    2021-01-13 01:54

    This is happening because, using @EnableWebMvc disables MVC autoconfiguration and asks you to provide exactly what You want. Have a look at this link
    You can either use other means to customize your configuration, such as a @Configuration See Boot's WebMvcAutoConfiguration to find out what the defaults are, and copy over the pieces that you need.

    This Link might help You as well -> LINK

    What should you do when you want to customize your beans? As usual, extend WebMvcConfigurerAdapter (annotate the new class with @Component) and do your customizations.
    So, bottom line of the particular problem: Don’t use @EnableWebMvc in Spring Boot, just include spring-web as a maven/gradle dependency and it will be autoconfigured.

    This answer on stackoverflow shows how to do this .. check this LINK3

提交回复
热议问题