客套话我就不说了,网上很多,我接下来主要做的是实现方式,以及容易出现问题的地方,因为是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>