Java spring 注释属性

Java spring annotation attribute

我有一个 post 方法接收一个对象作为参数,在这个对象中我有一个带有注释 @ValidDate 和 @NotEmpty 的属性。
在另一种方法中,我想使用相同的对象,但我只想在属性上注释@ValidDate。 有可能吗?

属性:

@NotEmpty 
@ValidDate 
private String installDate;

函数:

public String findLinksByCriteria(@Valid @ModelAttribute LinkForm link, BindingResult bindingResult, Model uiModel) {
    if (bindingResult.hasErrors()) {
        return ViewConstants.LINK_SEARCH_VIEW;
    }

可能是其中之一,有多种解决方案。您可以删除可选约束并手动执行此操作,但是如果您想将其严格保持在 Bean 验证 API 的上下文中,那么您可以使用验证组来执行此操作。

https://docs.oracle.com/cd/E19798-01/821-1841/gkahp/index.html

Constraints may be added to one or more groups. Constraint groups are used to create subsets of constraints, so only certain constraints will be validated for a particular object. By default, all constraints are included in the Default constraint group.

通过使用 Spring 的 @Validated 注释而不是 @Valid,您可以指定一组或多组约束应用于任何给定的情况。

这里有一个详细的例子:

http://blog.codeleak.pl/2014/08/validation-groups-in-spring-mvc.html