简体   繁体   English

Java Spring 启动 Web 应用程序:处理 404 异常

[英]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.我正在阅读 Java Spring 引导教程,并且正在尝试处理 404 异常。 I have the exact code as in the tutorial, but for some reason it isn't working, although the 403 error is working.我有教程中的确切代码,但由于某种原因它不工作,虽然 403 错误工作。 In the tutorial, the way the instructor handles all outstanding exceptions is with the following, specifically defaultErrorHandler().在本教程中,讲师处理所有未决异常的方式如下,特别是 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:然后,我尝试添加类似于讲师处理 403 错误的内容,如下所示,但这也不起作用:

    @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.我尝试通过将 System.out.println("test") 放入每个可疑方法中来进行调试,但都没有达到。 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.如果您想处理 404 异常以显示您的自定义页面而不是可白标错误。您可以通过在资源目录中创建名为 error 的文件夹来实现。在错误文件夹中创建 404.html 文件并添加 html 代码、样式等。 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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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