用于检测get请求的时候对其进行判空处理

用于检测get请求的时候对其进行判空处理

1、新建两个注解

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface NotNullGetRequest {

    String value();

}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface CheckParamController {
}

2、写aop,可以根据自己的需要,再写判断长度的

@Slf4j
@Aspect
@Component
public class NotNullGetRequestAop {

    @Pointcut("@annotation(com.richstonedt.cmgde2eas.commons.annotation.CheckParamController)")
    public void paramCheck(){}

    /**
     * @description: controller上标记了 {@link CheckParamController}的注解的会去
     *              检查字段上加 {@link NotNullGetRequest } 是否为空
     * @param joinPoint 对象
     * @Date 2023/3/16 上午 10:31
     */
    @Before("paramCheck())")
    public void doBefore(JoinPoint joinPoint) {
            if(joinPoint.getArgs().length > 0){//先确定是否要参数接收
                Object target = joinPoint.getArgs()[0];
                Field[] declaredFields = target.getClass().getDeclaredFields();
                Class<?> aClass = target.getClass();
                Arrays.stream(declaredFields).forEach(field->{
                    if (field.isAnnotationPresent(NotNullGetRequest.class)){
                        Object invoke = null;
                        try {
                            invoke = aClass.getDeclaredMethod("get" + BasicUtils.getFirstToUpperCase(field.getName()))
                                    .invoke(target);
                        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
                            log.error(ex.getMessage());
                        }
                        if (invoke instanceof List && ((List) invoke).isEmpty()){
                            throw new BusinessException(field.getAnnotation(NotNullGetRequest.class).value());
                        }
                        if (invoke == null || invoke.equals("")){//判断是否为空,为空就抛出异常
                            throw new BusinessException(field.getAnnotation(NotNullGetRequest.class).value());
                        }
                    }
                });
            }
    }



}

3、用法,现在实体类标记,之后再到controller中标记就好了

    @Schema(title = "开始时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @NotNullGetRequest("开始时间不能为空")
    private LocalDateTime beginTime;

    @Schema(title = "结束时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @NotNullGetRequest("结束时间不能为空")
    private LocalDateTime endTime;
    @GetMapping("/getData")
    @CheckParamController
    public ResponseResult<PageInfo<User>> getData(UserParams params){
        return ResponseResult.ok(service.page(params));
    }

测试

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值