java 如何使用基于组的@Validated 注释验证方法参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31826981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:11:31  来源:igfitidea点击:

How to validate an method argument with @Validated annotation based on group

javaspringvalidation

提问by mayank vats

I have an entity XYZ.java

我有一个实体 XYZ.java

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class XYZ extends BaseEntity implements Serializable {

  @NotNull (groups = {Groups.Insert.class, Groups.Delete.class, Groups.Update.class, Groups.Select.class})
  private String X;

and there is an interface XYZCRUD.javato do CRUD operations on XYZ.java

并且有一个接口XYZCRUD.java可以进行 CRUD 操作XYZ.java

@Validated
public interface XYZCRUD {

  public int insert(@Valid XYZ entity) throws SomeException;

Although javax's @Valid works for @NotNull validation but it does not support passing validation group as annotation attribute from the method from where i am triggering the validation . So I tried using @Validated annotation which does allow to pass groups via an attribute "value" like this

尽管 javax 的 @Valid 适用于 @NotNull 验证,但它不支持将验证组作为注释属性从我触发验证的方法中传递。所以我尝试使用 @Validated 注释,它允许通过这样的属性“值”传递组

@Validated
public interface XYZCRUD {

      public int insert(@Validated(value=SomeGroup.class) XYZ entity) throws SomeException;

However it does not trigger the validation at all.I tried after removing the groups attribute from the field and also from the trigger annotation.

但是它根本不会触发验证。我在从字段和触发器注释中删除组属性后尝试过。

Conclusion :@Validated does not trigger @NotNull

结论:@Validated 不会触发@NotNull

Questions :

问题 :

  1. Why @Validated does not trigger the javax's @NotNull ? Am I doing something wrong or does it not support it?
  2. Is there any any other way? cannot implement custom validator
  1. 为什么 @Validated 不会触发 javax 的 @NotNull ?我做错了什么还是不支持它?
  2. 有没有其他办法?无法实现自定义验证器

NOTE :I am also using lombok if it has something to do with this.

注意:如果与此有关,我也在使用 lombok。

采纳答案by mayank vats

It seems like @Validated was doing its job which is only to mark "parameters" with groups but how is that interpreted ("intercepted") by validation framework is a different story . In my case this is what was happening when method level validation were being intercerpted and validated

似乎@Validated 正在做它的工作,它只是用组标记“参数”,但验证框架如何解释(“拦截”)是另一回事。在我的情况下,这就是方法级验证被截取和验证时发生的情况

From the docs of Spring's MethodValidationInterceptor

来自 Spring 的 MethodValidationInterceptor 文档

Validation groups can be specified through Spring's Validated annotation at the type level of the containing target class, applying to all public service methods of that class. By default, JSR-303 will validate against its default group only.

验证组可以通过 Spring 的 Validated annotation 在包含目标类的类型级别指定,应用于该类的所有公共服务方法。默认情况下,JSR-303 将仅针对其默认组进行验证。

Which means the ONLYgroups mentioned at class level will be validated and they will be used for all public methods.

这意味着在类级别提到的唯一组将被验证,它们将用于所有公共方法

@Validated(value=SomeGroup.class)//this will be used as group for all methods being validated
public interface XYZCRUD {

public int insert(@Valid XYZ entity) throws SomeException;
public int delete(@Valid XYZ entity) throws SomeException;

I do not understand the reason for such implementation but this is what the docs say .If someone knows the reason please comment.

我不明白这种实现的原因,但这就是文档所说的。如果有人知道原因,请发表评论。

回答by rubensa

@mayank-vats If you want to apply a group to specific method (not all public methods), you can use:

@mayank-vats 如果要将组应用于特定方法(不是所有公共方法),可以使用:

@Validated
public class MyClass {

    @Validated({Group1.class})
    public myMethod1(@Valid Foo foo) { ... }

    @Validated({Group2.class})
    public myMethod2(@Valid Foo foo) { ... }

    ...
}

回答by Nerdfest

For @NotNull, you'll need to add the defgault group:

对于@NotNull,您需要添加 defgault 组:

@Validated({ Default.class, Group1.class })
public myMethod1(@NotNull @Valid Foo foo) { ... }