springMVC global exception configuration

  Normally development projects if the code is wrong, it may be displayed on the page in a detailed exception error message and are targeted to their project code in detail in the first few lines of an error may also appear on the show page, like this is not conducive to security code.

  So often that implement the interface HandlerExceptionResolver method:

. 1  / ** 
2  * @author god eat tea
 . 3  * 0010 pm @date 2020/1/10 16:53
 . 4   * / 
. 5  @Component
 . 6  public  class ExceptionResolver the implements the HandlerExceptionResolver {
 . 7  
. 8      Private  static Logger Logger LoggerFactory.getLogger = ( . ExceptionResolver class );
 . 9  
10      @Override
 . 11      public ModelAndView the resolveException (the HttpServletRequest Request, Response the HttpServletResponse, Object Handler, exception ex) {
 12 is          // Description of this URI abnormality has occurred, and the abnormal printed to the console through the above ex
13 is          logger.error ( "Exception {}" , Request.getRequestURI (), EX);
 14          ModelAndView ModelAndView = new new ModelAndView ( new new MappingJackson2JsonView ());
 15          modelAndView.addObject ( "Status" , ResponseCode.ERROR.getCode ()) ;
 16          modelAndView.addObject ( "MSG", "abnormal interfaces, service log details, please see" );
 . 17          modelAndView.addObject ( "Data" , ex.ToString ());
 18 is          return ModelAndView;
 . 19      }
 20 is }

  Effect :

Guess you like

Origin www.cnblogs.com/dashenaichicha/p/12178071.html