Spring Boot Rest - How to configure 404 - resource not found

前端 未结 2 1840
夕颜
夕颜 2021-02-04 07:46

I got a working spring boot rest service. When the path is wrong it doesn\'t return anything. No response At all. At the same time it doesn\'t throw error either. Ideally I expe

2条回答
  •  再見小時候
    2021-02-04 07:56

    I know this is an old question but here is another way to configure the DispatcherServlet in code but not in the main class. You can use a separate @Configuration class:

    @EnableWebMvc
    @Configuration
    public class ExceptionHandlingConfig {
    
        @Autowired
        private DispatcherServlet dispatcherServlet;
    
        @PostConstruct
        private void configureDispatcherServlet() {
            dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
        }
    }
    

    Please not that this does not work without the @EnableWebMvc annotation.

提交回复
热议问题