hutoo工具提供的字段校验工具

hutool工具对Hibernate Validator进行了简单包装,在手动字段校验中还是不错的,推荐使用。

public interface ValidationDefaultVO extends Default {
}

@Data
public class CaseV3ResourceTrackDTO {

    @NotNull
    private Integer id;

    @NotBlank
    private String content;

    private Integer userId;
}
 public static void main(String[] args) {
        CaseV3ResourceTrackDTO caseV3ResourceTrackDTO = new CaseV3ResourceTrackDTO();
        BeanValidationResult beanValidationResult = ValidationUtil.warpValidate(caseV3ResourceTrackDTO, ValidationDefaultVO.class);
        System.out.println(JSONUtil.toJsonStr(beanValidationResult));
    }

结果:
16:08:54.169 marketAppApi [main] INFO  o.h.validator.internal.util.Version - HV000001: Hibernate Validator 6.1.5.Final
{"errorMessages":[{"message":"不能为null","propertyName":"id"},{"message":"不能为空","propertyName":"content"}],"success":false}

Process finished with exit code 0

你可能感兴趣的:(java,java,spring,开发语言)