Search code examples
spring-bootvaadin8

How to redirect springboot error page to vaadin error UI?


I have an error page in vaadin UI. But sometimes we can write a wrong url and we see the springboot error page.

I want to show vaadin UI error page in this case. By the way, I have already have a rendered 404 page for springboot. But I don't want to show it.

This is my vaadin error UI. But this works into the application. (http://localhost:7001/MyApplication/#!error)

sometimes I write an invalid url like this: http://localhost:7001/MyApplication/blablablabla

in this case I want to redirect vaadin error page (http://localhost:7001/MyApplication/#!error) But the springboot redirects me to rendered 404 page.

It is possible?

@UIScope
@SpringView(name = ErrorView.VIEW_NAME)
public class ErrorView extends VerticalLayout implements View {

    public static final String VIEW_NAME = "error";

    private Label explanation;

    public ErrorView() {
        Label header = new Label("The view could not be found");
        header.addStyleName(MaterialTheme.LABEL_H1);
        addComponent(header);
        addComponent(explanation = new Label());
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        explanation.setValue(String.format("You tried to navigate to a view ('%s') that does not exist.", event.getViewName()));
        getUI().getNavigator().navigateTo(ErrorView.VIEW_NAME);
    }
}

Solution

  • you can put 404/500 page under resources/error/ folder, Spring boot will redirect those page automatic when have error