Hibernate如何验证内部类属性

时间:2018-05-14 08:50:57

标签: hibernate validation inner-classes

这是我的代码,我用注释标记了不起作用的部分

@Data
public class PopularTravelProductsDTO extends MobileRequest {

    @NotNull  //work
    private PopularTravelProductsInfo popularTravelProductsInfo;

    @Data
    public class PopularTravelProductsInfo {

        @NotNull //doesn't work
        private Integer pageNum;

        @NotNull //doesn't work
        private Integer size;
    }
}

1 个答案:

答案 0 :(得分:0)

使用@Valid注释

@Data
public class PopularTravelProductsDTO extends MobileRequest {

    @NotNull  
    @Valid
    private PopularTravelProductsInfo popularTravelProductsInfo;

    @Data
    public class PopularTravelProductsInfo {

        @NotNull 
        private Integer pageNum;

        @NotNull 
        private Integer size;
    }
}