Search code examples
javajspspring-mvchttp-redirectcustom-error-pages

No mapping found for HTTP request with URI while redirect


I have registered following viewResolvewer:

     <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

and wrote following controller method:

    @ExceptionHandler(value = Exception.class)
    public String redirectToErrorPage(){
        return "redirect:/errorPage";
    }

When following method executes I see following log:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/errorPage] in DispatcherServlet with name 'appServlet'

page with name errorPage.jsp locates under the page folder: enter image description here

Please explain what do I wrong?


Solution

  • You are redirecting to the the URL "/errorPage", but there is no request mapping for /errorPage. You can add a controller with that request mapping e.g.:

    @Controller
    public class ErrorPageController {
    
        @RequestMapping("/errorPage")
        @ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
        public String showErrorPage(){
            return "errorPage";
        }
    }
    

    Or instead of redirecting, you can just show the error page from your exception handler.

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleException(Exception e){
        return "errorPage";
    }