Java class 上的自定义注释作为字段(HAS-A 关系)

Java custom annotation on class as field (HAS-A relation)

要求 - 将输入验证为字母数字。 我有两个 class 在 Has-A 关系中

快速帮助感谢,提前致谢。

public class Party {
    @InputContraint
    private String firstName;
    @InputContraint
    private String lastName;
    
    private Address address;
}

public class Address {
    @InputContraint
    private String countryCode;
    @InputContraint
    private String countryName;
}


@Documented
@Constraint(validatedBy = InputValidate.class)
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface InputContraint {
    String message() default "error.message";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

public class InputValidate implements ConstraintValidator<InputContraint, String> {
    @Override
    public boolean isValid(String inputVal, ConstraintValidatorContext context) {
        if (Utilities.isEmpty(inputVal))
            return true;
        //
        String regex = "^[a-zA-Z0-9]+$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(inputVal);
        boolean valid = matcher.matches();
        return valid;
        }
}

Another way - still not working.
---
public class Address {
     @Pattern(regexp ="^[a-zA-Z0-9]+$")
     private String countryCode;
}
  1. 我已创建自定义注释并在字段级别应用

    • 如果字段为 String,它工作正常。
    • 如果归档为 Class,则无效。
  2. 已在 Address.countryCode

    应用 @Pattern(regex="^[a-zA-Z0-9]+$")

当您使用 @Valid 时,它会递归查看是否存在更多有效值并验证它们。我还为 pojo classes 添加了 getter 方法,没有 getter 方法似乎不起作用。 下面,我只是添加了 @Valid 的使用方法,并在 InputValidate 中做了一些调整。 请注意 @Valid 的导入来自 import javax.validation.Valid;

成功验证的请求正文应如下所示:

{
    "firstName":"John",
    "lastName": "Doe",
    "address": {
        "countryCode":"01",
        "countryName":"United State"
    }
    
}

控制器实现如下:

@RestController
@RequestMapping("/")
class TestController {  

        @PostMapping("/party")
        public String valid(@Valid @RequestBody Party party) {
            return "all valid";
        }
}

你的验证class如下:

@Slf4j
public class InputValidate implements ConstraintValidator<InputContraint, String> {
    @Override
    public boolean isValid(String inputVal, ConstraintValidatorContext context) {
        if(inputVal != null) {
            String regex = "^[a-zA-Z0-9]+$";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(inputVal);
            boolean valid = matcher.matches();
            log.info("InputValidate {} is {}", inputVal, valid);
            return valid;
        } else {
            return false;
        }
    }
}

聚会class

public class Address {
    @InputContraint
    private String countryCode;

    @InputContraint
    private String countryName;

    public String getCountryCode() {
        return countryCode;
    }

    public String getCountryName() {
        return countryName;
    }
}

地址class


public class Party {
    @InputContraint
    private String firstName;

    @InputContraint
    private String lastName;

    @Valid
    private Address address;

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public Address getAddress() {
        return address;
    }
}

参考:

https://www.sitepoint.com/java-bean-validation-object-graphs-valid-annotation/