SpringMVC中使用@ControllerAdvice详解

客套话我就不说了,网上很多,我接下来主要做的是实现方式,以及容易出现问题的地方,因为是springMVC搭建的,这个东西我搞了两天。我这个例子是以统一异常处理。网上看了很多都说的不够详细,走了很多弯路。好了,我们开始吧。

1.创建一个springMVC项目,引入需要的pom依赖,注意有两个包类似,web和webmvc,很重要!

        <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-webmvc</artifactId>
   	 		<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
       <dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.0</version>
			<scope>compile</scope>
		</dependency>

2.异常处理基类 

注意下面的注解,一个不能少,我就是缺了@EnableWebMvc 搞了很久,如果是springboot就不用这个注解

package com.miguan.station.tools.exception;
import java.util.HashMap;
import java.util.Map;

import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/***
 * 异常统一处理类
 * @author XiangGuoShuai
 * @Date 2020-05-18
 * 
 */
//@EnableWebMvc不用这个了,实现WebMvcConfigurer代替,因为有坑,原因自己百度
@ControllerAdvice
@ResponseBody
public class BaseExceptionHandler extends HashMap<String, Object> implements     WebMvcConfigurer{

	private static final long serialVersionUID = 1L;

	@ExceptionHandler(value = Exception.class)
    public Map errorHandler(Exception ex) {

        if(ex instanceof CustomException){
        	CustomExceptionexception = (CustomException) ex;
            this.put("msg",exception.getMsg());
            this.put("code", exception.getCode() == 0?403:exception.getCode());
        }else if(ex instanceof HttpMessageNotReadableException){
        	this.put("code", 504);
        	this.put("msg","参数缺失异常");
        }else if(ex instanceof HttpRequestMethodNotSupportedException){
        	this.put("code", 404);
        	this.put("msg","请求方法异常");
        }else {
        	this.put("code", 500);
        	this.put("msg","糟糕!系统发生不明异常");
        }
        return this;
    }

}

3.自定义实体类

package com.miguan.station.tools.exception;



/**
 * 自定义异常实体类
 */
public class CustomException extends RuntimeException {

	private static final long serialVersionUID = 1L;
	private int code;
    private String msg;

    public BIZException(int code, String msg){
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public BIZException(String msg){
        super(msg);
        this.msg = msg;
    }

	public long getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
    
}

4.开始写Controller吧。

package com.cn;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	
	//系统异常测试
	@GetMapping("/aaa")
	public int aaa() {
		int a = 5/0;
		return a;
		
	}
	
	//自定义异常测试
	@GetMapping("/bbb")
	public int bbb() {

		throw new CustomException("自定义异常了");
		
	}
	
}

 

5.测试结果

6.再提醒一点,如果你这个是一个jar包提供给别的项目引用,需要要基类加入到当前项目的bean去管理,不然也会不行的。如下:

<!-- 注入异常基类 -->
    <bean  class="com.cn.BaseExceptionHandler"></bean>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值