创建注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Created by niugang on 2019/11/23/16:27
*/
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomNotEmpty {
//错误信息
String value() default "参数不能为空";
}
AOP注解解析
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
* 自定义参数校验注解
*
* @author Created by niugang on 2019/11/23/16:28
*/
@Component
@Aspect
public class CustomNotEmptyAop {
@Before("execution(* *..service..*Service*.*(..))")
public Object aroundAdvice(JoinPoint pjd) throws ParamIsEmptyException {
MethodSignature signature = (MethodSignature) pjd.getSignature();
//得到拦截的方法
Method method = signature.getMethod();
String className = method.getDeclaringClass().getName();
String methodName = className + "." + method.getName();
//获取方法参数注解,返回二维数组是因为某些参数可能存在多个注解
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
if (parameterAnnotations == null || parameterAnnotations.length == 0) {
return pjd;
}
//获取方法参数名
String[] paramNames = signature.getParameterNames();
//获取参数值
Object[] paramValues = pjd.getArgs();
//获取方法参数类型
Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < parameterAnnotations.length; i++) {
for (int j = 0; j < parameterAnnotations[i].length; j++) {
Annotation annotation = parameterAnnotations[i][j];
if (annotation instanceof CustomNotEmpty) {
paramIsNull(methodName, paramNames[i], paramValues[i], parameterTypes[i] == null
? null
: parameterTypes[i].getName());
break;
}
}
}
return pjd;
}
/**
* 参数非空校验,如果参数为空,则抛出ParamIsEmptyException异常
*
* @param methodName methodName
* @param paramName paramName
* @param value value
* @param parameterType parameterType
*/
private void paramIsNull(String methodName, String paramName, Object value, String parameterType) throws ParamIsEmptyException {
if (value == null || "".equals(value.toString().trim())) {
throw new ParamIsEmptyException(methodName, paramName, parameterType);
}
}
}
自定义异常
/**
* @author Created by niugang on 2019/11/23/17:02
*/
public class ParamIsEmptyException extends RuntimeException {
private String methodName;
private String parameterName;
private String parameterType;
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public String getParameterName() {
return parameterName;
}
public void setParameterName(String parameterName) {
this.parameterName = parameterName;
}
public String getParameterType() {
return parameterType;
}
public void setParameterType(String parameterType) {
this.parameterType = parameterType;
}
public ParamIsEmptyException(String methodName, String parameterName, String parameterType) {
super();
this.methodName = methodName;
this.parameterName = parameterName;
this.parameterType = parameterType;
}
@Override
public String getMessage() {
return "方法[ " + this.methodName + "]" + "请求参数[" + this.parameterName + "]不能为空";
}
}
最终全局异常处理
import org.springframework.http.HttpStatus;
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.bind.annotation.ResponseStatus;
import java.util.HashMap;
import java.util.Map;
/**
* @author Created by niugang on 2019/11/24/9:28
*/
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = ParamIsEmptyException.class)
public Map<String, Object> handleException(Exception e) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("status", HttpStatus.BAD_REQUEST.value());
resultMap.put("message", "Illegal parameter");
return resultMap;
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = Exception.class)
public Map<String, Object> handleException2(Exception e) {
Map<String, Object> resultMap = new HashMap<>(16);
resultMap.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
resultMap.put("message", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
return resultMap;
}
}
微信公众号
JAVA程序猿成长之路
分享资源,记录程序猿成长点滴。专注于Java,Spring,SpringBoot,SpringCloud,分布式,微服务。