zoukankan      html  css  js  c++  java
  • Spring MVC异常处理

    1、@ExceptionHandler 进行局部的异常处理

    package controller;
    
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import exception.ExceptionUtil;
    
    @Controller
    public class ExceptionController {
    	
    	private static Logger logger = LogManager.getLogger(ExceptionController.class.getName());
    	
    	@RequestMapping("/exception/{id}")
    	@ResponseBody
    	public Object hello(@PathVariable String id) {
    		
    		if(id.equals("1")) {
    			int i = 1/0;
    			return null;
    		} else if(id.equals("2")) {
    			
    			throw new NullPointerException("空指针异常");
    			
    		} else if(id.equals("3")) {
    			throw new ExceptionUtil("自定义异常");
    		}
    		else {
    			return "ok";
    		}
    
    	}
    	
    	/**
    	 * 处理局部的ArithmeticException异常
    	 * @return
    	 */
        @ExceptionHandler(ArithmeticException.class)
        @ResponseBody
        public String handleArithmeticException(){
            return "ArithmeticException";
        }
        
        /**
         * 处理局部的NullPointerException异常
         * @return
         */
        @ExceptionHandler(NullPointerException.class)
        @ResponseBody
        public String handleNullPointerException(){
            return "NullPointerException";
        }
        
        /**
         * 处理自定义异常
         * @return
         */
        @ExceptionHandler(ExceptionUtil.class)
        @ResponseBody
        public String handleExceptionUtil() {
        	return "ExceptionUtil";
        }
        	
    }
    

    ExceptionUtil的具体代码为

    package exception;
    
    public class ExceptionUtil extends RuntimeException {
    	public ExceptionUtil(String string) {
    		super(string);
    	}
    }

    运行结果

    2、@ControllerAdvice+@ExceptionHandler 构建全局异常处理

    首先定义一个全局异常处理类MyGlobalExceptionHandler,该类加上了@ControllerAdvice注解,同时使用@ExceptionHandler对不同异常类的处理方法进行注解

    @ControllerAdvice
    public class MyGlobalExceptionHandler {
    	
    	/**
    	 * 处理局部的ArithmeticException异常
    	 * @return
    	 */
        @ExceptionHandler(ArithmeticException.class)
        @ResponseBody
        public String handleArithmeticException(){
            return "ArithmeticException";
        }
        
        /**
         * 处理局部的NullPointerException异常
         * @return
         */
        @ExceptionHandler(NullPointerException.class)
        @ResponseBody
        public String handleNullPointerException(){
            return "NullPointerException";
        }
        
        /**
         * 处理自定义异常
         * @return
         */
        @ExceptionHandler(ExceptionUtil.class)
        @ResponseBody
        public String handleExceptionUtil() {
        	return "ExceptionUtil";
        }
    }
    

      接着需要在SpringContext.xml文件中对MyGlobalExceptionHandler所在包进行扫描

    <context:component-scan base-package="handler" />
    

      运行结果

    参考:

    1、https://blog.csdn.net/qq_29914837/article/details/82697089

    2、https://www.cnblogs.com/lenve/p/10748453.html

    3、https://www.cnblogs.com/stamp/p/allex.html

    4、https://www.cnblogs.com/Coder-Pig/p/7340694.html

  • 相关阅读:
    python中删除某个元素的3种方法
    研发团队开源管理工具最佳实践
    Cocos2dx游戏开发系列笔记13:一个横版拳击游戏Demo完结篇
    Sunny谈软件架构
    整理部分JS 控件 WEB前端常用的做成Jsp项目,方便今后直接用
    hdu3033I love sneakers! (分组背包,错了很多次)
    Using关键字的用法
    Android webViewj简单处理apk的下载链接
    山寨腾讯“爱消除”游戏之菜单特效
    Apache与Nginx优缺点比较
  • 原文地址:https://www.cnblogs.com/wylwyl/p/13283237.html
Copyright © 2011-2022 走看看