Vaadin 14 Route hide URL

how to disable (not to visible) all Routes if I enter or make mistake in URL path? it offers list of all Routes which isnt for me secure and why it should be visible

Hi Petr

When you enter a URL that does not match a route of your defined views, the Router throws a NotFoundException. You can define your own View/Layout that appears when that happens. If you don’t, then the default one from Vaadin will be used, which is the one you see - with all the existing routes being listed (the class is called RouteNotFoundError). How to define your own Exception Handlers is covered in the documentation, please check it out: https://vaadin.com/docs/v14/flow/routing/tutorial-routing-exception-handling.html

Here is an example class that - when you add it to your vaadin project - will display only a basic message instead of showing all existing routes:

@ParentLayout(MainView.class) // optionally, a parent layout can be defined.
public class NotFoundExceptionHandler extends VerticalLayout implements HasErrorParameter<NotFoundException> {
    @Override
    public int setErrorParameter(BeforeEnterEvent event, ErrorParameter<NotFoundException> parameter) {
        add(new Label(String.format("The page %s does not exist.", event.getLocation().getPath())));
        return HttpServletResponse.SC_NOT_FOUND;
    }
}

thanks for explaining how it works by default, yes thats what I want to do, I have Error page in Thymeleaf, and want show it if part URL is bad(not REST urls), so I have to make RouterException and map somehow to my error page, if I understand?

Note that the list of all routes is only shown in development mode, not in production mode.

Petr Mikolas:
thanks for explaining how it works by default, yes thats what I want to do, I have Error page in Thymeleaf, and want show it if part URL is bad(not REST urls), so I have to make RouterException and map somehow to my error page, if I understand?

you don’t have to throw any NotFoundException yourself, that is already happening. And you also don’t have to “map somehow to your error page”, as this is already automatically done by vaadin, when you let your exception handler layout implements HasErrorParameter<NotFoundException>. Vaadin will scan your project upon startup, it sees this error handler and will redirect to it once a NotFoundException is thrown.

Olli Tietäväinen:
Note that the list of all routes is only shown in development mode, not in production mode.

I though too in DEV mode ok, but after mvn clean package -Pproduction and add to Heroku PROD mode I see still all available list of routes

Kaspar Scherrer:

Petr Mikolas:
thanks for explaining how it works by default, yes thats what I want to do, I have Error page in Thymeleaf, and want show it if part URL is bad(not REST urls), so I have to make RouterException and map somehow to my error page, if I understand?

you don’t have to throw any NotFoundException yourself, that is already happening. And you also don’t have to “map somehow to your error page”, as this is already automatically done by vaadin, when you let your exception handler layout implements HasErrorParameter<NotFoundException>. Vaadin will scan your project upon startup, it sees this error handler and will redirect to it once a NotFoundException is thrown.

ok I understand thanks for help it sounds good