简体   繁体   中英

Java Spring Boot Web App: Handling 404 Exception

I'm going through a Java Spring Boot tutorial and am in the middle of trying to handle 404 exceptions. I have the exact code as in the tutorial, but for some reason it isn't working, although the 403 error is working. In the tutorial, the way the instructor handles all outstanding exceptions is with the following, specifically defaultErrorHandler(). However, this is not working:

@ControllerAdvice
public class GlobalExceptionHandler {

    @Value("${message.error.exception}")
    private String exceptionMessage;

    @Value("${message.error.duplicate.user}")
    private String duplicateUserMessage;

    @ExceptionHandler(value=Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.getModel().put("message", exceptionMessage);
        modelAndView.getModel().put("url", req.getRequestURL());
        modelAndView.getModel().put("exception", e);
        modelAndView.setViewName("app.exception");
        return modelAndView;
    }

    @ExceptionHandler(value=DataIntegrityViolationException.class)
    public ModelAndView duplicateUserHandler(HttpServletRequest req, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.getModel().put("message", duplicateUserMessage);
        modelAndView.getModel().put("url", req.getRequestURL());
        modelAndView.getModel().put("exception", e);
        modelAndView.setViewName("app.exception");
        return modelAndView;
    }
}

I then tried to add something similar to what the instructor has for handling 403 errors, found below, but this is not working either:

    @RequestMapping("/403")
    ModelAndView accessDenied(ModelAndView modelAndView) {
        System.out.println("We're here");
        modelAndView.getModel().put("message", accessDeniedMessage);
        modelAndView.setViewName("app.message");
        return modelAndView;

    }

    @RequestMapping("/404")
    ModelAndView pageNotFound(ModelAndView modelAndView) {

        modelAndView.getModel().put("message", exceptionMessage);
        modelAndView.setViewName("app.message");
        return modelAndView;

    }

I've tried to debug by putting System.out.println("test") in each of the suspected methods, but none of them have been reached. Any help would be appreciated. Thanks!

If You wanna Handle 404 exception to show your custom page rather than whitelable error.You can do that by creating folder named error in resources directory.In error folder create 404.html file and add html code,style,etc. Now Spring Boot picks your 404.html file and displays to user whenever 404 error occurs.Like this you can also handle any errors just putting filename of error code 501.html,407.html etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM