这篇我们来学习公共异常的处理,不管项目使用spring_boot 还是 spring+spring_mvc 处理方式都是一样的。
一,由于前篇我们定义了aop,这里需要先注释掉doAround,doAfterThrowing两个方法,抛出的异常统一由公共异常处理
二,我们定义一个基类,作为返回到页面的数据
package com.example.base;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import com.example.constant.ReturnConstant;
/**
* 统一返回数据
* @author Administrator
*
*/
public class ReturnResult implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7475919483473633240L;
private String status = ReturnConstant.RETURN_OK;
private String msg;
private Map<String,Object> result = new HashMap<String,Object>();
public ReturnResult (String msg) {
this.msg = msg;
}
public ReturnResult (String status , String msg) {
this.status = status;
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Map<String, Object> getResult() {
return result;
}
public void setResult(Map<String, Object> result) {
this.result = result;
}
@Override
public String toString() {
return "ReturnResult [status=" + status + ", msg=" + msg + ", result=" + result + "]";
}
}
三,定义自定义异常类
package com.example.exception;
import com.example.constant.ReturnConstant;
/**
* 自定义异常
* @author Administrator
*
*/
public class AppException extends RuntimeException{
/**
*
*/
private static final long serialVersionUID = -6228059312180101897L;
private String status = ReturnConstant.RETURN_OK;
private String msg;
public AppException (String msg) {
this.msg = msg;
}
public AppException (String status,String msg) {
this.status = status;
this.msg = msg;
}
public AppException (String status,String msg,Exception ex) {
super(ex);
this.status = status;
this.msg = msg;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
四,定义公共异常处理类
在方法上添加注解@ResponseBody,将异常信息封装后返回到页面
package com.example.exception;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.base.ReturnResult;
@ControllerAdvice
public class CommonExceptiomHandler {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
public ReturnResult illegalArgumentException(IllegalArgumentException e) {
logger.error(e.getMessage());
return new ReturnResult("param error");
}
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseBody
public ReturnResult MissingServletRequestParameterException(MissingServletRequestParameterException e) {
logger.error(e.getMessage());
return new ReturnResult("missing servlet request");
}
@ExceptionHandler(TypeMismatchException.class)
@ResponseBody
public ReturnResult typeMismatchException(TypeMismatchException e) {
logger.error(e.getMessage());
return new ReturnResult("type mismatch error");
}
@ExceptionHandler(AppException.class)
@ResponseBody
public ReturnResult appException(AppException e) {
logger.error(e.getMessage());
return new ReturnResult(e.getStatus(),e.getMsg());
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody
public ReturnResult httpRequestMethodNotSupportedException(Exception e) {
logger.error(e.getMessage());
return new ReturnResult("http request method not supported");
}
@ExceptionHandler(Exception.class)
@ResponseBody
public ReturnResult defaultException(Exception e) {
logger.error(e.getMessage());
return new ReturnResult("system error");
}
}
五,测试,HelloController
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.base.ReturnResult;
import com.example.exception.AppException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RestController
public class HelloController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@RequestMapping("/hello/exception")
public ReturnResult helloException(){
logger.info("=== hello world exception");
throw new AppException("test app exception");
}
}
六,启动项目测试:
http://localhost:8088/spring-boot/hello/exception
查看返回结果:
{
"status": "true",
"msg": "test app exception",
"result": {}
}
====================================================================================
下篇我们学习缓存框架,SpringBoot 学习记录(七): Redis