SpringBoot 学习记录(六): Exception

这篇我们来学习公共异常的处理,不管项目使用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





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值