Error while Handling 404 error for Spring Boot REST application

跟風遠走 提交于 2019-12-04 16:51:07
Manu

I have added the mapping to redirect to custom html pages if a 404 happens. PFB the changes done for this:

  • Removed the Annotation @EnableWebMvc from the WebConfig.java class. This is done to remove the error “response already committed”, on trying any invalid url.
  • Add the below code in WebConfig.java class and the revenant html pages:

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
    
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
    
                ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED,
                        "/401.html");
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND,
                        "/404.html");
                ErrorPage error500Page = new ErrorPage(
                        HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
                ErrorPage error505Page = new ErrorPage(
                        HttpStatus.HTTP_VERSION_NOT_SUPPORTED, "/505.html");
                ErrorPage error506Page = new ErrorPage(
                        HttpStatus.METHOD_NOT_ALLOWED, "/405.html");
                container.addErrorPages(error401Page, error404Page,
                        error500Page, error505Page, error506Page);
            }
        };
    }
    

Thanks a lot for the suggestions ans hep. It was very useful.

I followed this link after getting through the initial exeception

You can follow the below blog to handle mvc exception handling

http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

Upgrade to Spring Boot 1.1.5.RELEASE.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!