javax validation on nested object with groups

為{幸葍}努か 提交于 2020-01-15 05:55:29

问题


Here is my java code

class AbstractO{
     public interface Save{}; // for groups
     public interface Update{}; // for groups
}
class A extends AbstractO{
     public Integer id;
     @NotNull(groups={A.Save.class})
     @Valid
     public B b;
}
class B extends AbstractO{
     @NotNull(groups={B.Update.class})
     public Integer id;
     @NotNull(groups={A.Save.class})
     public Integer prop

}

// controller method

@ResponseBody
public ResponseEntity<A> save(@RequestBody @Validated(value = { A.Save.class }) A ,
            BindingResult bindingResult) {
// code goes here...
}

Issue

At time of saving A I want to have validation. Validation group is set as A.Save in controller method so B b in class A is is getting validated as notnull.

However I want to have validation of prop of class B while saving A, So I have added @Valid on B b and mentioned group A.Save on prop so that validate that only when A is getting saved.

My problem is even id of B has on B.Update as group, it still validates in controller (with group A.Save). May be issue is the @Valid annotation but without that annotation it does not validates nested things.

来源:https://stackoverflow.com/questions/41457404/javax-validation-on-nested-object-with-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!