我正在尝试创建注释,并将变量作为参数传递给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
请协助