Search code examples
springspring-boothttp-status-code-404vaadinhttp-status-code-500

vaadin + spring boot: Cannot forward to error page for request


By single view application vaadin 7.7.7, spring-boot 1.5 i check uri fragment https:/tld/#!category-name-1 from user and if the category exist show items and if not

VaadinService.getCurrentResponse().sendError(404, "page not found!");

but i got error after update spring-boot 1.5 and vaadin 7.7.7 (with embeded tomcat):

Cannot forward to error page for request [/vaadinServlet/UIDL/] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

How can i send http error pages from vaadin to user?

ErrorPageCutomizer.java

@Component
public class ErrorPageCutomizer implements EmbeddedServletContainerCustomizer {
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));
        container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
    }
}

RestController.java

import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ErrorHandlingController implements ErrorController {

    private static final String PATH = "/error";

    @RequestMapping(value = PATH + "/404")
    public String error404() {
        return "<div style='font-weight:bold; margin-top:200px; text-align:center; font-size:160%;'>Page not found...<br><a href=\"https://tld\">to home</a></div>";
    }

    @RequestMapping(value = PATH + "/500")
    public String error500() {
        return "<div style='font-weight:bold; margin-top:200px; text-align:center; font-size:160%;'>500 Internal server error...</div>";
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }
}

Solution

  • Soliution was:

     @Configuration
    public class AppInitializer implements WebApplicationInitializer {
    
        @Bean
        public ErrorPageFilter errorPageFilter() {
            return new ErrorPageFilter();
        }
    
        @Bean
        public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
            filterRegistrationBean.setFilter(filter);
            filterRegistrationBean.setEnabled(false);
            return filterRegistrationBean;
        }
    }