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

posted @ 2020-07-11 13:07  林木声  阅读(174)  评论(0编辑  收藏  举报