1.自定义工具
2.自定义异常处理
3.全局异常处理
效果图
案例一
案例二
再也不用写
if (availableByUid == null) {
throw new IllegalArgumentException("该月卡限额已用完");
}
以后直接写
SysUtils.isNotNullExp(availableByUid,"该月卡限额已用完");
自定义工具
import lombok.experimental.UtilityClass;
import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
/**
* <p>
* 系统工具类
* </p>
* @author: Aiyex
*/
@UtilityClass
public class SysUtils {
/**
* 判断object是否为空,集合会校验size
* @param objs
* @return boolean
*/
public boolean isNull(Object... objs) {
for (Object obj : objs) {
if (isEmpty(obj)) {
return true;
}
}
return false;
}
/**
* 不是空的 抛出异常
*/
public void isNullExp(Object obj,String msg){
if (isNull(obj)) {
return;
}
throw new ServiceException(msg);
}
/**
* 是空的 抛出异常
*/
public void isNotNullExp(Object obj,String msg){
if (!isNull(obj)) {
return;
}
throw new ServiceException(msg);
}
/**
* 非true 抛出异常
*/
public void isTrueExp(Boolean isTrue,String msg){
if (isTrue) {
return;
}
throw new ServiceException(msg);
}
/**
* true 抛出异常
*/
public void isFalseExp(Boolean isTrue,String msg){
if (!isTrue) {
return;
}
throw new ServiceException(msg);
}
/**
* 判断object是否不为空,集合会校验size
* @param obj
* @return boolean
*/
public boolean isNotNull(Object... obj) {
return !isNull(obj);
}
/**
* 对象非空判断
* @param obj
* @return boolean
*/
public boolean isNotEmpty(Object obj) {
return !isEmpty(obj);
}
/**
* 对象空判断
* @param obj
* @return boolean
*/
public boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj.getClass().isArray()) {
return Array.getLength(obj) == 0;
}
if (obj instanceof CharSequence) {
return ((CharSequence) obj).length() == 0;
}
if (obj instanceof Collection) {
return ((Collection) obj).isEmpty();
}
if (obj instanceof Map) {
return ((Map) obj).isEmpty();
}
return false;
}
}
注意 ServiceException 是自己定义的异常处理类
自定义异常处理
/**
* 业务异常
*
* @author Aiyex
*/
public final class ServiceException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 错误码
*/
private Integer code;
/**
* 错误提示
*/
private String message;
/**
* 错误明细,内部调试错误
*
*/
private String detailMessage;
/**
* 空构造方法,避免反序列化问题
*/
public ServiceException() {
}
public ServiceException(String message) {
this.message = message;
}
public ServiceException(String message, Integer code) {
this.message = message;
this.code = code;
}
public String getDetailMessage()
{
return detailMessage;
}
@Override
public String getMessage()
{
return message;
}
public Integer getCode()
{
return code;
}
public ServiceException setMessage(String message) {
this.message = message;
return this;
}
public ServiceException setDetailMessage(String detailMessage) {
this.detailMessage = detailMessage;
return this;
}
}
全局异常处理
/**
* 全局异常处理器
* @author Aiyex
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 业务异常 --- 主要是这个
*/
@ExceptionHandler(ServiceException.class)
public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) {
log.error(e.getMessage(), e);
Integer code = e.getCode();
return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
}
/**
* 拦截未知的运行时异常
*/
@ExceptionHandler(RuntimeException.class)
public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生未知异常.", requestURI, e);
return AjaxResult.error(e.getMessage());
}
/**
* 系统异常
*/
@ExceptionHandler(Exception.class)
public AjaxResult handleException(Exception e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生系统异常.", requestURI, e);
return AjaxResult.error(e.getMessage());
}
}