Spring bean validation with custom constraint using @Valid on controller

I hope someone would have some ideas, because I’m stuck at this for 2 days now… I have a restAPI (not my project, I’m unable to modify it) that was recently updated by the owning team. Let’s say I have the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@PostMapping("/api/post")
ResponseEntity<?> post(HttpServletRequest httpServletRequest, @Valid PostParameter postParameter) {
...
}
</code>
<code>@PostMapping("/api/post") ResponseEntity<?> post(HttpServletRequest httpServletRequest, @Valid PostParameter postParameter) { ... } </code>
@PostMapping("/api/post")
ResponseEntity<?> post(HttpServletRequest httpServletRequest, @Valid PostParameter postParameter) {
    ...
}

Where PostParameter is a base class, which I’m extending. Here comes the problem. Certain fields of this class needs to be validated based on properties that the application was started with. For simplicity’s sake let’s say I have this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public final class ResourceCreateParameter extends PostParameter {
@CustomMessageConstraint
private final String message;
// omitted
}
</code>
<code>public final class ResourceCreateParameter extends PostParameter { @CustomMessageConstraint private final String message; // omitted } </code>
public final class ResourceCreateParameter extends PostParameter {
    @CustomMessageConstraint
    private final String message;
    // omitted
}

where I naively implemented my constraint as:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Target({ElementType.FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = CustomMessageValidator.class)
public @interface CustomMessageConstraint {
String message() default "Message is not allowed"
Class<?>[] group() default {};
Class<? extends Payload>[] payload() default {};
}
</code>
<code>@Target({ElementType.FIELD}) @Retention(RUNTIME) @Constraint(validatedBy = CustomMessageValidator.class) public @interface CustomMessageConstraint { String message() default "Message is not allowed" Class<?>[] group() default {}; Class<? extends Payload>[] payload() default {}; } </code>
@Target({ElementType.FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = CustomMessageValidator.class)
public @interface CustomMessageConstraint {
    String message() default "Message is not allowed"
    Class<?>[] group() default {};
    Class<? extends Payload>[] payload() default {};
}

and my validator is again fairly simple:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Component
public class CustomMessageValidator
implements ConstraintValidator<CustomMessageConstraint, String> {
private final String[] allowedValues;
public CustomMessageValidator(ValidatorProperties validatorProperties) {
this.allowedValues = validatorProperties.allowedValues();
}
@Override
public boolean isValid(String s, ConstraintValidatorContext context) {
return Arrays.stream(allowedValues).anyMatch(v -> v.equalsIgnoreCase(s));
}
}
</code>
<code>@Component public class CustomMessageValidator implements ConstraintValidator<CustomMessageConstraint, String> { private final String[] allowedValues; public CustomMessageValidator(ValidatorProperties validatorProperties) { this.allowedValues = validatorProperties.allowedValues(); } @Override public boolean isValid(String s, ConstraintValidatorContext context) { return Arrays.stream(allowedValues).anyMatch(v -> v.equalsIgnoreCase(s)); } } </code>
@Component
public class CustomMessageValidator 
        implements ConstraintValidator<CustomMessageConstraint, String> {
    private final String[] allowedValues;

    public CustomMessageValidator(ValidatorProperties validatorProperties) {
        this.allowedValues = validatorProperties.allowedValues();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext context) {
        return Arrays.stream(allowedValues).anyMatch(v -> v.equalsIgnoreCase(s));
    }
}

Upon trying it out however, allowedValues is null as DI is not taking effect. I’ve also tried defining LocalValidatorFactory bean, but @Valid still refuses to do the validation. The only way I could validate is by modifying the (in my case unmodifyable piece of code) aka the controller. There, I can inject an instance of CustomMessageValidator (even without LocalValidatorFactory bean present) and I can manually trigger .validate method.

Is there a way, around it? I need to use the ConfigurationProperties to read the value that is allowed, and the validation is handled via @Valid.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật