springboot3中成员变量如何做validation校验?

有两个对象A和B,两个对象中的参数都加上了 jakarta.validation的@NotNull注解,如果在C类中有两个成员变量private A a;private B b,提交参数时如何让controller校验生效

public RetJson<Void> calculate(@Valid @Validated @RequestBody C c){.....}
public class A {
    @NotNull(message = "A's field cannot be empty")
    private Integer fieldA;
 
    // getters and setters
}
 
public class B {
    @NotNull(message = "B's field cannot be empty")
    private Integer fieldB;
 
    // getters and setters
}
public class C {
    @Valid
    private A a;
 
    @Valid
    private B b;
 
    // getters and setters
}
阅读 589
1 个回答

问了deepseek解决了,加了这个就生效了@NotNull(message = "xxx不能为空")

public class C {
    @Valid
@NotNull(message = "xxx不能为空")
    private A a;
 
    @Valid
@NotNull(message = "xxx不能为空")
    private B b;
 
    // getters and setters
}
推荐问题