自定义注解的使用(将request参数中的数据解释成对象)

项目中,每个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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值