使用注释传递值

时间:2020-07-13 14:29:38

标签: java spring spring-boot spring-aop spring-aspects

我正在尝试创建注释,并将变量作为参数传递给impl方法。下面是我创建注释的代码。

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;

import javax.validation.Constraint;

@Documented
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = Loggable1Impl.class)
public @interface Loggable1 {
    String message() default "Invalid userId";
}

下面是我的展示课程

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class Loggable1Impl implements ConstraintValidator<Loggable, String> {
    
    public void initialize(Loggable contactNumber) {
    }

    public boolean isValid(String generatedUserId, ConstraintValidatorContext cxt) {
        System.out.println("generatedUserId in Loggable1 :: " + generatedUserId);
        return true;
    }
}

我正在尝试在春季启动应用程序中使用它

@CrossOrigin(origins = "*", allowedHeaders = "*")
@PostMapping("/api1/{user}")
public Topic regiterUser(@PathVariable String user ) {
    String generatedUserId = user + "1";
    return generatedUserId ;
}

我想将generatedUserId传递给我的注释Loggable1

请协助

0 个答案:

没有答案