项目中,每个controller方法都需要验证用户登录,这里工程写了一个简单的注解,来将cookie中的用户信息还原成对象
注解类
package com.cheshangma.operation.wx.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
//@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
//@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得
//@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
@Documented
public @interface ObjectConvertAnno {
/**
* 指定该参数的值是否允许为 null 默认是不允许 Whether the parameter is required.
* <p>
* Default is <code>true</code>, leading to an exception thrown in case of the parameter missing
* in the request. Switch this to <code>false</code> if you prefer a <code>null</value> in case of
* the parameter missing.
*/
boolean required() default true;
}
然后写注解的实现方法
package com.cheshangma.operation.wx.configuration;
import javax.servlet.http.Htt