1定义一个在方法上作用的注解
@Retention(RetentionPolicy.RUNTIME) // 表示注解在运行时依然存在
@Target(ElementType.METHOD)
@Documented
public @interface noNull {
public String str() default "";
}
2定义一个拦截类,拦截被注解的方法,并验证注解中指定的方法是否为空
public class NoNullInterceptor extends HandlerInterceptorAdapter{
//在请求处理之前进行调用(Controller方法调用之前
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
//如果不是映射到方法直接通过
if (!(o instanceof HandlerMethod)) {
return true;
}
HandlerMethod handlerMethod = (HandlerMethod) o;
Method method = handlerMethod.getMethod();
if (method.getAnnotation(noNull.class) != null) {
noNull noNullAnnotation=method.getAnnotation(noNull.class);
String Str = noNullAnnotation.str()