SoFunction
Updated on 2025-04-06

Use @Validate to group the parameters

@Validate Group Verification Parameters

public interface Group {

    /**
      * New operations
      */
    interface  ADD {}

    /**
      * Update operation
      */
    interface UPDATE {}

    /**
      * Update operation
      */
    interface DELETE {}
}
@Data
@EqualsAndHashCode(callSuper = false)
public class StudentInfoDto implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonSerialize(using = )
    @NotBlank(message = "The primary key cannot be empty", groups = {, })
    private String id;

    private String name;
}
@PostMapping(value = "/delete")
public Result delete(@RequestBody @Validated(value = ) final StudentInfoDto studentInfoDto) {
    return (());
}

@Validated annotation is an annotation provided by Spring Framework for method-level parameter verification.

It is usually used in conjunction with the Bean Validation (JSR-380) specification for validating method parameters.

This annotation can be placed on the method's parameter list, indicating that the parameter needs to be verified.

In the @Validated(value = ) you mentioned, value = is a parameter, which is used to specify the Validation Group used for verification.

Verification packets can be used to perform different verification logic in different scenarios.

Here, it may be a custom validation grouping class that defines the validation rules that need to be performed when performing a delete operation.

Give an example

Suppose there is a method for deleting users, and the deletion operation can only be performed when the user's permissions meet certain conditions.

Then you can define a verification group for deletion operations, which contains verification rules for user permissions.

When the method parameters are marked with @Validated(value = ) annotation, the method parameters are verified using the specified verification group.

Need to pay attention

The @Validated annotation is provided by the Spring framework, unlike the @Valid annotation, which is annotation in the Bean Validation (JSR-380) specification.

Although they can all be used for parameter verification, the @Validated annotation is more flexible and supports functions such as group verification.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.