强烈推荐一个大神的人工智能的教程:http://www.captainai.net/zhanghan
【前言】
处理好异常对系统有很好的保护作用同时会大大提高用户的体验,对异常统一处理是一个非常实用的技巧,接下来介绍一下如何在系统中对异常进行统一处理;
【统一异常处理】
一、未加统一异常处理时
启动程序,并访问http://localhost:8080/swagger-ui.html#/,中的lombok演示为例返回结果如下图所示,可见返回值是十分的不友好。
二、进行改造:
增加全局异常类:
/*
* Copyright (c) 2019. zhanghan_java@163.com All Rights Reserved.
* 项目名称:实战SpringBoot
* 类名称:GlobalExceptionHandler.java
* 创建人:张晗
* 联系方式:zhanghan_java@163.com
* 开源地址: https://github.com/dangnianchuntian/springboot
* 博客地址: https://blog.csdn.net/zhanghan18333611647
*/
package com.zhanghan.zhboot.filter;
import com.zhanghan.zhboot.util.wrapper.WrapMapper;
import com.zhanghan.zhboot.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 全局的的异常拦截器
*
* @author https://blog.csdn.net/zhanghan18333611647
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* IllegalArgumentException
*
* @param e the e
* @return the wrapper
*/
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Wrapper illegalArgumentException(IllegalArgumentException e) {
log.error("IllegalArgumentException={}", e.getMessage(), e);
return WrapMapper.error("param error");
}
/**
* IllegalStateException
*
* @param e the e
* @return the wrapper
*/
@ExceptionHandler(IllegalStateException.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Wrapper IllegalStateException(IllegalStateException e) {
log.error("IllegalStateException={}", e.getMessage(), e);
return WrapMapper.error();
}
/**
* HttpMessageNotWritableException
*
* @param e the e
* @return the wrapper
*/
@ExceptionHandler(HttpMessageNotWritableException.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Wrapper HttpMessageNotWritableException(HttpMessageNotWritableException e) {
log.error("HttpMessageNotWritableException={}", e.getMessage(), e);
return WrapMapper.error();
}
/**
* HttpMessageNotReadableException
*
* @param e the e
* @return the wrapper
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Wrapper HttpMessageNotReadableException(HttpMessageNotReadableException e) {
log.error("HttpMessageNotReadableException={}", e.getMessage(), e);
return WrapMapper.error();
}
/**
* ConversionFailedException
*
* @param e the e
* @return the wrapper
*/
@ExceptionHandler(ConversionFailedException.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Wrapper ConversionFailedException(ConversionFailedException e) {
log.error("ConversionFailedException={}", e.getMessage(), e);
return WrapMapper.error();
}
/**
* BindException
*
* @param e the e
* @return the wrapper
*/
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Wrapper BindException(BindException e) {
log.error("BindException={}", e.getMessage(), e);
return WrapMapper.error();
}
/**
* MethodArgumentNotValidException
*
* @param e the e
* @return the wrapper
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Wrapper validationError(MethodArgumentNotValidException e) {
log.error("MethodArgumentNotValidException={}", e.getMessage(), e);
return WrapMapper.error();
}
/**
* Exception
*
* @param e the e
* @return the wrapper
*/
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Wrapper exception(Exception e) {
log.error("Exception={}", e.getMessage(), e);
return WrapMapper.error();
}
}
三、改造后的效果:
四、项目地址及代码版本:
1、地址:https://github.com/dangnianchuntian/springboot
2、代码版本:1.3.0-Release
【总结】
1、提高用户的体验度;
2、统一处理大大减少开发的维护工作。