AOP实现参数的判空问题
不想每次都去判断必传的参数是否为空,写代码太繁琐了,正好最近用了AOP实现权限控制,依葫芦画瓢,现在用它实现参数的判空,至于AOP的原理之类,自己百度了解一下吧
1. NullDisable注解
@Documented
@Retention(RUNTIME)
@Target({ TYPE, METHOD, PARAMETER })
public @interface NullDisable { }
2. ParamException
public class ParamException extends RuntimeException{
private static final long serialVersionUID = -4993447045204262508L;
public ParamException(){
super("参数不能为空");
}
public ParamException(String message){
super(message);
}
}
3. ValidParameter
import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component; import com.test.exception.ParamException; @Aspect
@Component
public class ValidParameter {
//com.test.controller包下所有的类
@Pointcut("execution(* com.test.controller..*.*(..)))")
public void valid() {}; @Around("valid()")
public Object check(ProceedingJoinPoint joinPoint) throws Exception{ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
//获得参数类型
final Parameter[] parameters = method.getParameters();
//参数值
final Object[] args = joinPoint.getArgs();
//参数名称
String[] names = signature.getParameterNames(); for(int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
Object annotation = parameter.getAnnotation(NullDisable.class);
//含有不为空的注解的参数
if (null != annotation) {
if (null == args[i]) {
throw new ParamException(String.format("参数:%s,不能为空", names[i]));
}
} }
return joinPoint.proceed();
}
}
2. controller
@GetMapping("test")
@PermissionSetter
public Object test(
@RequestParam(value = "name") String name,
@NullDisable @RequestParam(value = "date") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime date
){
return "";
}
postman测试

AOP实现参数的判空问题的更多相关文章
- 在Java中如何优雅地判空
判空灾难 作为搬砖党的一族们,我们对判空一定再熟悉不过了,不要跟我说你很少进行判空,除非你喜欢NullPointerException. 不过NullPointerException对于很多猿们来 ...
- java中对对象进行判空的操作--简洁编码
java中对对象进行判空的操作 首先来看一下工具StringUtils的判断方法: 一种是org.apache.commons.lang3包下的: 另一种是org.springframework.ut ...
- 《SpringBoot判空处理》接开@valid的面纱
一.事有起因 我们在与前端交互的时候,一般会遇到字段格式校验及非空非null的校验,在没有SpringBoot注解的时候, 我们可能会在service进行处理: if(null == name){ t ...
- Asp.net webapi 判断请求参数是否为空简易方法 Model Validation 判断请求参数是否为空
通常情况下,对于那些经常为别人提供数据接口的开发人员来说,对于调用方传递过来的参数都会有验证处理.例如: if (string.IsNullOrEmpty(entity.Name)) { //当姓名为 ...
- shell 字符串判空
2021-09-01 1. 字符串判空主要用到两个参数 -z 判断字符串为空否 -n 判断字符串不为空 2. 实例 #!/bin/bash PID=`date` if [ -z "$PID& ...
- String工具类之“四个判空方式”StringUtils.isNotBlank和StringUtils.isEmpty和StringUtils.isBlank和StringUtils.isNotEmpty
一.判断str字符串都不为空==>StringUtils.isNotBlank(String str); 1 /** 2 * <p>检查一个字符串是否非空("") ...
- 使用Jayrock开源组件创建参数可为空的接口
经过上一篇文章对Jayrock开源组件的分析,我发现了一个问题,就是在写接口的时候,可以设置某个参数为空,可以不需要进行参数的传递,具体写法如下: 图上的test参数就是可空类型,只需标识为int?类 ...
- java中判空
一.概述 java中判等似乎很简单,==用来判断对象引用(内存地址)是否相同,equals用来判断值是否相同.你可以试用String对象轻松区分这一点. 那么在null判等(也就是判空操作)时呢? 可 ...
- JSTL: empty 可以减少很多繁冗的判空(转)
${empty student.name }Empty是判空为空返回的真不为空返回的是假 ${(empty student.name)? '空' : '非空'} <c:if test=" ...
随机推荐
- excel 类获取起始列和使用列
m_excel.OpenWorkBook(sFileName, sSheetDrawingList); // Get drawing info int iStartRow = 0, iStartCol ...
- tomcat8安装及配置
首先是解压版的安装.很简单,直接解压到要安装的位置就OK了. 2.启动 bin目录下,执行startup.bat文件 3.浏览器中打开地址http://localhost:8080/
- C lstat major MAJOR 获得设备号
#cat lstat.c #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #inc ...
- stress工具使用指南和结果分析
stress介绍 #stress `stress' imposes certain types of compute stress on your system Usage: stress [OPTI ...
- JavaScript学习笔记之DOM介绍
目录 1.简介 2.方法 3.属性 4.访问节点 5.修改节点 6.添加节点 7.删除节点 8.替换节点 9.改变 CSS 1.简介 文档对象模型(Document Object Model,DOM) ...
- 我的ArcGis9.3 到Arcgis10.0 升级步骤
因为之前一直安装的是Arcgis 9.3 版本,领导发了个10.0版本说,该升级了,结果就开始了漫漫的升级路. 个人操作过程,只是个别. 一.卸载Arcgis9.3 这个过程真说是艰辛啊. 首先,卸载 ...
- Ac自动机基础题集合
Ac_automaton的板子打熟以后发现碰到题不会做,而且还是比较纯的板子,只要改几处地方就可以,Ac_automation有许多优秀而fantasy的性质,下面粘几个题,来记录一下做题的心得. 1 ...
- ansible plugins 列表
[action plugins] [cache plugins]jsonfilememcachedmemorymongodbpickleredisyaml [callback plugins]acti ...
- fzu 2124
#include<stdio.h> #include<queue> #include<math.h> #include<algorithm> #incl ...
- ACDream - Xor pairs
先上题目: Xor pairs Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Sub ...