我的自定义验证注释在 Spring 引导中不起作用

My custom validation annotation is not working in Spring Boot

我创建了一个注释,它具有检查输入的字符串是否为空或在分配的 value.But 注释不起作用等功能。我不认为它对 DTO.What 做任何验证我做错了吗?我还在控制器上添加了有效注释。我认为 isValid() 函数不起作用。

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;

import static java.lang.annotation.ElementType.PARAMETER;

@Documented
@Constraint(validatedBy = {SpecificStringValueValidator.class})
@Target( {PARAMETER} )
@Retention(RetentionPolicy.RUNTIME)
public @interface SpecificStringValue {
    //This annotation ensures input is not null and contains no empty strings
    //Also applies maximum minimum control if specified
    //Checks if the input is of length value given.
    int value() default 0;
    int min() default -9999;
    int max() default -9999;
    String message() default "Invalid String";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

public class SpecificStringValueValidator implements ConstraintValidator<SpecificStringValue,String> {

    private SpecificStringValue specificValue;
    private boolean status = false;

    @Override
    public void initialize(SpecificStringValue specificValue) {
        ConstraintValidator.super.initialize(specificValue);
        this.specificValue = specificValue;
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        String str = s;
        String noSpaceStr = str.replaceAll("\s", "");

        //This if block work if it is requested to have a certain length.
        if(specificValue.value() != 0){
            //Check does the string contain a space character?
            if(noSpaceStr.length() == s.length() && s.isEmpty() && s.length() == specificValue.value()){
                status = true;
            }
        }
        //If there is no specific value, it checks the max min.
        else {
            //Check does the string contain a space character?
            if(noSpaceStr.length() == s.length() && s.isEmpty() ){
               //Check the minimum number
                if(specificValue.min() != -9999){
                    if(s.length() >= specificValue.min()){
                        status = true;
                    }
                }
                //Check the maximum number
                if(specificValue.max() != -9999){
                    if(s.length() <= specificValue.max()){
                        status = true;
                    }
                }
            }
        }
        return status;
    }
}
public record CreateSellerRequestDTO(
@SpecificStringValue(value = 5,message = "Username cant be null") userName){}
@PostMapping
    public ResponseEntity<?> create(@RequestBody @Valid CreateSellerRequestDTO createSellerRequestDTO){
            sellerService.create(createSellerRequestDTO);
            return ResponseEntity.ok().build();
    }

尝试将 org.springframework.boot:spring-boot-starter-validation 依赖项添加到您的项目。

顺便说一句,您的 isValid() 方法存在多个问题 - 您没有考虑输入字符串为 null 并且似乎使用 s.isEmpty() 而不是 !s.isEmpty()

记录 类 实际上是 auto-generated 类 具有不可变字段和每个字段的 getter-style 方法。你怎么知道注释 @SpecificStringValue 放在哪里(在方法或字段上)?

注释的目标只是 PARAMETER 所以我怀疑注释没有传播到字段 (userName),这将触发验证。您至少应该尝试将 METHODFIELD 添加到注释目标