Spring-mvc controller and exception handling

后端 未结 5 1545
星月不相逢
星月不相逢 2021-02-09 08:35

Would like to ask you a best practice question where a spring-mvc controller is concerned. Please review the code below:

    @Autowired
    SomeService service;
         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-09 09:18

    Define bean in bean definition file for Handler class. when any exception is thrown in a program ,resolveException method is called.

      public class Handler
            implements HandlerExceptionResolver
        {
    
            public Handler()
            {
            }
    
            public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            {
                if(ex instanceof ErrorType1Exception))
                {
                     ModelAndView test = new ModelAndView("errorpage1jsppage");
    return test;
                } else
                if(ex instanceof ErrorType2Exception))
                {
                     ModelAndView test1 = new ModelAndView("errorpage2jsppage");
    return test1
                }
            }
        }
    

提交回复
热议问题