Home>

I'm new to Spring.
When a local site is launched with Spring boot and a URL without mapping settings is entered (random URL, ex: http: // localhost/nvewoncavewhiev, etc.)
I wanted to make it a 404 error, but especially on the console side, no error or execution occurs, and the screen transitions to a blank screen.

Currently, I think it is better to let the interceptor judge the response.sendRedirect ("/ error");part as a 404 error.

  public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if (handler instanceof HandlerMethod) {
            String rootPath = request.getRequestURI ();
/ * Various processing * /
        response.sendRedirect ("/ error");
        return false;
    }

Usage environment: Eclipse 2020-09 (4.17.0)
Java SE 11
Spring boot 2.3.1

What I want to do in the end is to display a unique error screen for each status code such as 404 and 500.

First of all, I asked a question because it will not progress unless this is resolved.
I would like to ask for a professor.

  • Answer # 1

    It was solved by setting as follows.
    Thank you to everyone who taught me.
    response.sendRedirect ("/ error");→ response.sendError (HttpServletResponse.SC_NOT_FOUND);

  • Answer # 2

    Isn't this Qiita helpful? ??

    Customize the display when an error such as 404 Not Found occurs in Spring Boot