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

  • 相关阅读:
    2015531 网络攻防 Exp1 PC平台逆向破解(5)M
    2017-2018-1 20155331 嵌入式C语言
    20155330 《网络对抗》 Exp9 web安全基础实践
    20155330 《网络对抗》 Exp8 Web基础
    20155330 《网络对抗》 Exp7 网络欺诈防范
    20155330 《网络对抗》 Exp6 信息搜集与漏洞扫描
    20155330 《网络对抗》 Exp5 MSF基础应用
    20155330 《网络攻防》 Exp4 恶意代码分析
    20155330 《网络攻防》 Exp3 免杀原理与实践
    20155330 《网络对抗》 Exp2 后门原理与实践
  • 原文地址:https://www.cnblogs.com/wylwyl/p/13283237.html
Copyright © 2011-2022 走看看