需求:
1.自定义注解 ,用于标注需要进行校验的参数
2.AOP配合自定义注解使用
3.实现公共的返回参
4.实现全局异常捕获
所有的类
image.png

1.pom.xml依赖

  1. <!--aop-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-aop</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.aspectj</groupId>
  8. <artifactId>aspectjweaver</artifactId>
  9. <version>1.9.4</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.aspectj</groupId>
  13. <artifactId>aspectjrt</artifactId>
  14. <version>1.8.9</version>
  15. </dependency>
  16. <!--fastjson-->
  17. <dependency>
  18. <groupId>com.alibaba</groupId>
  19. <artifactId>fastjson</artifactId>
  20. <version>1.2.58</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>

2.新建自定义注解,ParamCheck.java :

  1. import java.lang.annotation.ElementType;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.RetentionPolicy;
  4. import java.lang.annotation.Target;
  5. /**
  6. * 自定义注解
  7. * @author beyond
  8. * @since 2021/4/11
  9. */
  10. @Target(ElementType.PARAMETER) // 使用于参数
  11. @Retention(RetentionPolicy.RUNTIME)
  12. public @interface ParamCheck {
  13. /**
  14. * 是否非空,默认不能为空
  15. * 要求参数不为空,默认开启,可以自己传
  16. */
  17. boolean notNull() default true;
  18. /***
  19. * 默认值
  20. */
  21. String defaultValue() default "";
  22. }

3.新建 参数校验的AOP实现类,ParamValidAop.java

  1. import org.aspectj.lang.JoinPoint;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.AfterReturning;
  4. import org.aspectj.lang.annotation.Around;
  5. import org.aspectj.lang.annotation.Aspect;
  6. import org.aspectj.lang.annotation.Before;
  7. import org.aspectj.lang.annotation.Pointcut;
  8. import org.aspectj.lang.reflect.MethodSignature;
  9. import org.springframework.stereotype.Component;
  10. import org.springframework.util.StringUtils;
  11. import java.lang.annotation.Annotation;
  12. import java.lang.reflect.Method;
  13. /**
  14. * 参数校验的AOP实现类
  15. * @author beyond
  16. * @since 2021/4/11
  17. */
  18. @Component
  19. @Aspect
  20. public class ParamValidAop {
  21. /**
  22. * 定义有一个切入点,范围为web包下的类
  23. */
  24. @Pointcut("execution(public * com.beyond.demo.controller..*.*(..))")
  25. public void checkParam() {
  26. }
  27. @Before("checkParam()")
  28. public void doBefore(JoinPoint joinPoint) {
  29. }
  30. /**
  31. * 检查参数是否为空
  32. */
  33. @Around("checkParam()")
  34. public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
  35. MethodSignature signature = ((MethodSignature) pjp.getSignature());
  36. //得到拦截的方法
  37. Method method = signature.getMethod();
  38. //获取方法参数注解,返回二维数组是因为某些参数可能存在多个注解
  39. Annotation[][] parameterAnnotations = method.getParameterAnnotations();
  40. if (parameterAnnotations == null || parameterAnnotations.length == 0) {
  41. return pjp.proceed();
  42. }
  43. //获取方法参数名
  44. String[] paramNames = signature.getParameterNames();
  45. //获取参数值
  46. Object[] paranValues = pjp.getArgs();
  47. //获取方法参数类型
  48. Class<?>[] parameterTypes = method.getParameterTypes();
  49. for (int i = 0; i < parameterAnnotations.length; i++) {
  50. for (int j = 0; j < parameterAnnotations[i].length; j++) {
  51. //如果该参数前面的注解不为空并且是ParamCheck的实例,并且notNull()=true,并且默认值为空,则进行非空校验
  52. if (parameterAnnotations[i][j] != null && parameterAnnotations[i][j] instanceof ParamCheck && ((ParamCheck) parameterAnnotations[i][j]).notNull() && StringUtils.isEmpty(((ParamCheck)parameterAnnotations[i][j]).defaultValue())) {
  53. paramIsNull(paramNames[i], paranValues[i], parameterTypes[i] == null ? null : parameterTypes[i].getName());
  54. break;
  55. }
  56. //如果该参数前面的注解不为空并且是ParamCheck的实例,并且默认值不为空,并且参数值为空,则进行赋默认值
  57. if(parameterAnnotations[i][j] != null && parameterAnnotations[i][j] instanceof ParamCheck && !StringUtils.isEmpty(((ParamCheck)parameterAnnotations[i][j]).defaultValue()) && (paranValues[i] == null || StringUtils.isEmpty(paranValues[i].toString()))){
  58. paranValues[i] = putParam(((ParamCheck)parameterAnnotations[i][j]).defaultValue(), parameterTypes[i]);
  59. }
  60. }
  61. }
  62. return pjp.proceed(paranValues);
  63. }
  64. /**
  65. * 在切入点return内容之后切入内容(可以用来对处理返回值做一些加工处理)
  66. *
  67. * @param joinPoint
  68. */
  69. @AfterReturning("checkParam()")
  70. public void doAfterReturning(JoinPoint joinPoint) {
  71. }
  72. /**
  73. * 参数非空校验,如果参数为空,则抛出ParamIsNullException异常
  74. * @param paramName
  75. * @param value
  76. * @param parameterType
  77. */
  78. private void paramIsNull(String paramName, Object value, String parameterType) {
  79. if (value == null || "".equals(value.toString().trim())) {
  80. throw new ParamIsNullException(paramName, parameterType,"参数为空");
  81. }
  82. }
  83. private Object putParam(Object value, Class<?> parameterType) {
  84. return CastValueTypeUtil.parseValue(parameterType, value.toString());
  85. }
  86. }

4.校验参数里面使用到的参数转换工具类,CastValueTypeUtil.java:

  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. /**
  5. * 转换object类型
  6. * @author beyond
  7. * @since 2021/4/11
  8. */
  9. public class CastValueTypeUtil {
  10. public static Object parseValue(Class<?> parameterTypes, String value) {
  11. if(value==null || value.trim().length()==0){
  12. return null;
  13. }
  14. value = value.trim();
  15. if (Byte.class.equals(parameterTypes) || Byte.TYPE.equals(parameterTypes)) {
  16. return parseByte(value);
  17. } else if (Boolean.class.equals(parameterTypes) || Boolean.TYPE.equals(parameterTypes)) {
  18. return parseBoolean(value);
  19. }/* else if (Character.class.equals(fieldType) || Character.TYPE.equals(fieldType)) {
  20. return value.toCharArray()[0];
  21. }*/ else if (String.class.equals(parameterTypes)) {
  22. return value;
  23. } else if (Short.class.equals(parameterTypes) || Short.TYPE.equals(parameterTypes)) {
  24. return parseShort(value);
  25. } else if (Integer.class.equals(parameterTypes) || Integer.TYPE.equals(parameterTypes)) {
  26. return parseInt(value);
  27. } else if (Long.class.equals(parameterTypes) || Long.TYPE.equals(parameterTypes)) {
  28. return parseLong(value);
  29. } else if (Float.class.equals(parameterTypes) || Float.TYPE.equals(parameterTypes)) {
  30. return parseFloat(value);
  31. } else if (Double.class.equals(parameterTypes) || Double.TYPE.equals(parameterTypes)) {
  32. return parseDouble(value);
  33. } else if (Date.class.equals(parameterTypes)) {
  34. return parseDate(value);
  35. } else {
  36. throw new RuntimeException("request illeagal type, type must be Integer not int Long not long etc, type=" + parameterTypes);
  37. }
  38. }
  39. public static Byte parseByte(String value) {
  40. try {
  41. value = value.replaceAll(" ", "");
  42. return Byte.valueOf(value);
  43. } catch(NumberFormatException e) {
  44. throw new RuntimeException("parseByte but input illegal input=" + value, e);
  45. }
  46. }
  47. public static Boolean parseBoolean(String value) {
  48. value = value.replaceAll(" ", "");
  49. if (Boolean.TRUE.toString().equalsIgnoreCase(value)) {
  50. return Boolean.TRUE;
  51. } else if (Boolean.FALSE.toString().equalsIgnoreCase(value)) {
  52. return Boolean.FALSE;
  53. } else {
  54. throw new RuntimeException("parseBoolean but input illegal input=" + value);
  55. }
  56. }
  57. public static Integer parseInt(String value) {
  58. try {
  59. value = value.replaceAll(" ", "");
  60. return Integer.valueOf(value);
  61. } catch(NumberFormatException e) {
  62. throw new RuntimeException("parseInt but input illegal input=" + value, e);
  63. }
  64. }
  65. public static Short parseShort(String value) {
  66. try {
  67. value = value.replaceAll(" ", "");
  68. return Short.valueOf(value);
  69. } catch(NumberFormatException e) {
  70. throw new RuntimeException("parseShort but input illegal input=" + value, e);
  71. }
  72. }
  73. public static Long parseLong(String value) {
  74. try {
  75. value = value.replaceAll(" ", "");
  76. return Long.valueOf(value);
  77. } catch(NumberFormatException e) {
  78. throw new RuntimeException("parseLong but input illegal input=" + value, e);
  79. }
  80. }
  81. public static Float parseFloat(String value) {
  82. try {
  83. value = value.replaceAll(" ", "");
  84. return Float.valueOf(value);
  85. } catch(NumberFormatException e) {
  86. throw new RuntimeException("parseFloat but input illegal input=" + value, e);
  87. }
  88. }
  89. public static Double parseDouble(String value) {
  90. try {
  91. value = value.replaceAll(" ", "");
  92. return Double.valueOf(value);
  93. } catch(NumberFormatException e) {
  94. throw new RuntimeException("parseDouble but input illegal input=" + value, e);
  95. }
  96. }
  97. public static Date parseDate(String value) {
  98. try {
  99. String datePattern = "yyyy-MM-dd HH:mm:ss";
  100. SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
  101. return dateFormat.parse(value);
  102. } catch(ParseException e) {
  103. throw new RuntimeException("parseDate but input illegal input=" + value, e);
  104. }
  105. }
  106. }

5. 新建一个自定义异常,专门用于校验参数为空的时候抛出,ParamIsNullException.java:

  1. package com.beyond.demo.paramCheck;
  2. /**
  3. * 自定义异常,用于校验参数为空的时候抛出
  4. *
  5. * @author beyond
  6. * @since 2021/4/11
  7. */
  8. public class ParamIsNullException extends RuntimeException {
  9. private final String parameterName;
  10. private final String parameterType;
  11. private final String message;
  12. public ParamIsNullException(String parameterName, String parameterType, String message) {
  13. super();
  14. this.parameterName = parameterName;
  15. this.parameterType = parameterType;
  16. this.message = message;
  17. }
  18. @Override
  19. public String getMessage() {
  20. return "请求参数类型:" + this.parameterType + ",参数名: \'" + this.parameterName + message;
  21. }
  22. public final String getParameterName() {
  23. return this.parameterName;
  24. }
  25. public final String getParameterType() {
  26. return this.parameterType;
  27. }
  28. }


至此,自定义注解以及AOP已经实现完毕

6.统一的返回参以及全局异常捕获

  1. /**
  2. * 此接口用于返回码枚举使用
  3. * @author beyond
  4. * @since 2021/4/11
  5. */
  6. public interface BaseErrorInfoInterface {
  7. /** 错误码*/
  8. String getResultCode();
  9. /** 错误描述*/
  10. String getResultMsg();
  11. }

返回码的枚举类,CommonEnum.java:

  1. /**
  2. * 返回码的枚举类
  3. *
  4. * @author beyond
  5. * @since 2021/4/11
  6. */
  7. public enum CommonEnum implements BaseErrorInfoInterface {
  8. // 数据操作错误定义
  9. SUCCESS("200", "成功!"),
  10. BODY_NOT_MATCH("400", "请求的数据格式不符!"),
  11. SIGNATURE_NOT_MATCH("401", "请求的数字签名不匹配!"),
  12. NOT_FOUND("404", "未找到该资源!"),
  13. INTERNAL_SERVER_ERROR("500", "服务器内部错误!"),
  14. SERVER_BUSY("503", "服务器正忙,请稍后再试!"),
  15. REQUEST_METHOD_SUPPORT_ERROR("40001", "当前请求方法不支持");
  16. /**
  17. * 错误码
  18. */
  19. private String resultCode;
  20. /**
  21. * 错误描述
  22. */
  23. private String resultMsg;
  24. CommonEnum(String resultCode, String resultMsg) {
  25. this.resultCode = resultCode;
  26. this.resultMsg = resultMsg;
  27. }
  28. @Override
  29. public String getResultCode() {
  30. return resultCode;
  31. }
  32. @Override
  33. public String getResultMsg() {
  34. return resultMsg;
  35. }
  36. }

定义一个简单的返回体专用类,ResultBody.java:

  1. package com.beyond.demo.paramCheck;
  2. import com.alibaba.fastjson.JSONObject;
  3. /**
  4. * 返回体专用类
  5. *
  6. * @author beyond
  7. * @since 2021/4/11
  8. */
  9. public class ResultBody {
  10. /**
  11. * 响应代码
  12. */
  13. private String code;
  14. /**
  15. * 响应消息
  16. */
  17. private String message;
  18. /**
  19. * 响应结果
  20. */
  21. private Object result;
  22. public ResultBody() {
  23. }
  24. public ResultBody(BaseErrorInfoInterface errorInfo) {
  25. this.code = errorInfo.getResultCode();
  26. this.message = errorInfo.getResultMsg();
  27. }
  28. public String getCode() {
  29. return code;
  30. }
  31. public void setCode(String code) {
  32. this.code = code;
  33. }
  34. public String getMessage() {
  35. return message;
  36. }
  37. public void setMessage(String message) {
  38. this.message = message;
  39. }
  40. public Object getResult() {
  41. return result;
  42. }
  43. public void setResult(Object result) {
  44. this.result = result;
  45. }
  46. /**
  47. * 成功
  48. *
  49. * @return
  50. */
  51. public static ResultBody success() {
  52. return success(null);
  53. }
  54. /**
  55. * 成功
  56. *
  57. * @param data
  58. * @return
  59. */
  60. public static ResultBody success(Object data) {
  61. ResultBody rb = new ResultBody();
  62. rb.setCode(CommonEnum.SUCCESS.getResultCode());
  63. rb.setMessage(CommonEnum.SUCCESS.getResultMsg());
  64. rb.setResult(data);
  65. return rb;
  66. }
  67. /**
  68. * 失败
  69. */
  70. public static ResultBody error(BaseErrorInfoInterface errorInfo) {
  71. ResultBody rb = new ResultBody();
  72. rb.setCode(errorInfo.getResultCode());
  73. rb.setMessage(errorInfo.getResultMsg());
  74. rb.setResult(null);
  75. return rb;
  76. }
  77. /**
  78. * 失败
  79. */
  80. public static ResultBody error(String code, String message) {
  81. ResultBody rb = new ResultBody();
  82. rb.setCode(code);
  83. rb.setMessage(message);
  84. rb.setResult(null);
  85. return rb;
  86. }
  87. /**
  88. * 失败
  89. */
  90. public static ResultBody error(String message) {
  91. ResultBody rb = new ResultBody();
  92. rb.setCode("-1");
  93. rb.setMessage(message);
  94. rb.setResult(null);
  95. return rb;
  96. }
  97. @Override
  98. public String toString() {
  99. return JSONObject.toJSONString(this);
  100. }
  101. }

自定义的业务异常类,和全局异常捕获类

  1. package com.beyond.demo.paramCheck;
  2. /**
  3. * 自定义的业务异常类
  4. *
  5. * @author beyond
  6. * @since 2021/4/11
  7. */
  8. public class BizException extends RuntimeException {
  9. private static final long serialVersionUID = 1L;
  10. /**
  11. * 错误码
  12. */
  13. protected String errorCode;
  14. /**
  15. * 错误信息
  16. */
  17. protected String errorMsg;
  18. public BizException() {
  19. super();
  20. }
  21. public BizException(BaseErrorInfoInterface errorInfoInterface) {
  22. super(errorInfoInterface.getResultCode());
  23. this.errorCode = errorInfoInterface.getResultCode();
  24. this.errorMsg = errorInfoInterface.getResultMsg();
  25. }
  26. public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
  27. super(errorInfoInterface.getResultCode(), cause);
  28. this.errorCode = errorInfoInterface.getResultCode();
  29. this.errorMsg = errorInfoInterface.getResultMsg();
  30. }
  31. public BizException(String errorMsg) {
  32. super(errorMsg);
  33. this.errorMsg = errorMsg;
  34. }
  35. public BizException(String errorCode, String errorMsg) {
  36. super(errorCode);
  37. this.errorCode = errorCode;
  38. this.errorMsg = errorMsg;
  39. }
  40. public BizException(String errorCode, String errorMsg, Throwable cause) {
  41. super(errorCode, cause);
  42. this.errorCode = errorCode;
  43. this.errorMsg = errorMsg;
  44. }
  45. public String getErrorCode() {
  46. return errorCode;
  47. }
  48. public void setErrorCode(String errorCode) {
  49. this.errorCode = errorCode;
  50. }
  51. public String getErrorMsg() {
  52. return errorMsg;
  53. }
  54. public void setErrorMsg(String errorMsg) {
  55. this.errorMsg = errorMsg;
  56. }
  57. public String getMessage() {
  58. return errorMsg;
  59. }
  60. @Override
  61. public Throwable fillInStackTrace() {
  62. return this;
  63. }
  64. }
  1. package com.beyond.demo.paramCheck;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.web.HttpRequestMethodNotSupportedException;
  5. import org.springframework.web.bind.MissingServletRequestParameterException;
  6. import org.springframework.web.bind.annotation.ExceptionHandler;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import org.springframework.web.bind.annotation.RestControllerAdvice;
  9. import javax.servlet.http.HttpServletRequest;
  10. /**
  11. * 全局异常捕获类
  12. * @author beyond
  13. * @since 2021/4/11
  14. */
  15. @RestControllerAdvice
  16. public class GlobalExceptionHandler {
  17. private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
  18. /**
  19. * 处理自定义的业务异常
  20. * @param req
  21. * @param e
  22. * @return
  23. */
  24. @ExceptionHandler(value = BizException.class)
  25. @ResponseBody
  26. public ResultBody bizExceptionHandler(HttpServletRequest req, BizException e){
  27. logger.error("发生业务异常!原因是:{}",e.getErrorMsg());
  28. return ResultBody.error(e.getErrorCode(),e.getErrorMsg());
  29. }
  30. /**
  31. * 处理空指针的异常
  32. * @param req
  33. * @param e
  34. * @return
  35. */
  36. @ExceptionHandler(value =NullPointerException.class)
  37. @ResponseBody
  38. public ResultBody exceptionHandler(HttpServletRequest req, NullPointerException e){
  39. logger.error("发生空指针异常!原因是:",e);
  40. return ResultBody.error(CommonEnum.BODY_NOT_MATCH);
  41. }
  42. /**
  43. * 处理请求方法不支持的异常
  44. * @param req
  45. * @param e
  46. * @return
  47. */
  48. @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
  49. @ResponseBody
  50. public ResultBody exceptionHandler2(HttpServletRequest req, HttpRequestMethodNotSupportedException e){
  51. logger.error("发生请求方法不支持异常!原因是:",e);
  52. return ResultBody.error(CommonEnum.REQUEST_METHOD_SUPPORT_ERROR);
  53. }
  54. /**
  55. * 处理请求方法不支持的异常
  56. * @param req
  57. * @param e
  58. * @return
  59. */
  60. @ExceptionHandler(value = {ParamIsNullException.class, MissingServletRequestParameterException.class})
  61. @ResponseBody
  62. public ResultBody exceptionHandler3(HttpServletRequest req, Exception e){
  63. logger.error("参数为空!原因是:",e);
  64. return ResultBody.error(CommonEnum.SIGNATURE_NOT_MATCH.getResultCode(),e.getMessage());
  65. }
  66. /**
  67. * 处理其他异常
  68. * @param req
  69. * @param e
  70. * @return
  71. */
  72. @ExceptionHandler(value =Exception.class)
  73. @ResponseBody
  74. public ResultBody exceptionHandler(HttpServletRequest req, Exception e){
  75. logger.error("未知异常!原因是:",e);
  76. return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
  77. }
  78. }

接下来看看这套实现的东西效果如何:
给需要的校验的参数加上我们的自定义注解即可,如:

  1. @GetMapping("/hello1")
  2. public ResultBody hello1(
  3. /*@ParamCheck(defaultValue = "banbu") */@ParamCheck String name) {
  4. return ResultBody.success();
  5. }

image.png
那么我们试试不传参数name: