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

  • 相关阅读:
    不同浏览器对于textarea中换行符的处理
    apache 页面压缩技术mod_deflate安装和配置
    php5+Apache2+mysql+Eclipse debug完全配置
    php程序的国际化(转载)
    PHP加载mysql DDL会失败
    mysql绿色版注册服务
    DEDE利用自由列表创建全站RSS订阅内容更新整站.xml文档
    PHP模拟多线程请求
    在Mac OS X中配置Apache + PHP + MySQL(转)
    ThinkPHP框架项目开发专题自动验证
  • 原文地址:https://www.cnblogs.com/wylwyl/p/13283237.html
Copyright © 2011-2022 走看看