java—在spring中,@valid如何捕获methodargumentnotvalidexception

z2acfund  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(230)

我有这个方法:

private File isFileValid(@Valid File file, Errors errors) {
        if (errors.hasErrors()) {
            List<ConstraintViolation<?>> violationsList = new ArrayList<>();
            for (ObjectError e : errors.getAllErrors()) {
                violationsList.add(e.unwrap(ConstraintViolation.class));
            }

            Map<String, Object> data = new HashMap<>();
            // hash map ? instead of stringbuilder 

            // Construct a helpful message for each input violation
            for (ConstraintViolation<?> violation : violationsList) {
                data.put(violation.getPropertyPath().toString(), violation.getMessage());
            }

            throw new CustomValidationException("Invalid fields!", data);
        }
        return file;
    }

我这样称呼它:

isFileValid(newFile);

调用方法时如何传递errors对象?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题