vaadin + spring boot: не удается перенаправить запрос на страницу с ошибкой

С помощью приложения с одним видом vaadin 7.7.7, spring-boot 1.5 я проверяю фрагмент uri https:/tld/#!category-name-1 от пользователя и, если категория существует, показывают элементы, а если нет

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

но я получил ошибку после обновления spring-boot 1.5 и vaadin 7.7.7 (со встроенным 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

Как я могу отправить страницы ошибок http от vaadin пользователю?

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;
    }
}

person tsogtgerel.ts    schedule 17.02.2017    source источник


Ответы (2)


Решение было:

 @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;
    }
}
person tsogtgerel.ts    schedule 17.03.2017
comment
Это не работает для Vaadin 8.0.5 и Spring boot 1.5.3. - person tsogtgerel.ts; 11.05.2017

Вы пробовали SystemMessagesProvider. В этом провайдере вы можете определить errorUrl для различных ошибок:

public class YourServlet extends VaadinServlet
{
    @Override
    protected void servletInitialized() throws ServletException
    {
        super.servletInitialized();

        getService().setSystemMessagesProvider(new SystemMessagesProvider()
        {
            @Override
            public SystemMessages getSystemMessages(SystemMessagesInfo systemMessagesInfo)
            {
                final CustomizedSystemMessages c = new CustomizedSystemMessages();
                final String errorUrl = "<url to errorpage>";
                c.setSessionExpiredURL(errorUrl);
                c.setSessionExpiredNotificationEnabled(false);

                c.setAuthenticationErrorURL(errorUrl);
                c.setAuthenticationErrorNotificationEnabled(false);

                c.setCommunicationErrorURL(errorUrl);
                c.setCommunicationErrorNotificationEnabled(false);

                c.setCookiesDisabledURL(errorUrl);
                c.setCookiesDisabledNotificationEnabled(false);

                c.setInternalErrorURL(errorUrl);
                c.setInternalErrorNotificationEnabled(false);

                c.setSessionExpiredURL(errorUrl);
                c.setSessionExpiredNotificationEnabled(false);
                return c;
            }
        });
    }
person Axel Meier    schedule 17.02.2017
comment
Вопрос был «Как я могу отправить страницы ошибок http от vaadin пользователю?». Не «Как я могу отправлять страницы ошибок с помощью Spring». И мое предложение именно в этом. Я был бы признателен за объяснение, почему мой ответ был понижен. - person Axel Meier; 20.02.2017
comment
если я отправлю: VaadinService.getCurrentResponse().sendError(404, страница не найдена!); получил следующую ошибку на консоли: Невозможно перенаправить на страницу ошибки для запроса [/], поскольку ответ уже был зафиксирован. В результате ответ может иметь неправильный код состояния. Если ваше приложение работает на WebSphere Application Server, вы можете решить эту проблему, установив для com.ibm.ws.webcontainer.invokeFlushAfterService значение false, настроив sys msg не так, как я имел в виду. - person tsogtgerel.ts; 17.03.2017