SpringBoot ????????????????????????

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot ????????????????????????相关的知识,希望对你有一定的参考价值。

???????????????   gis   erro   oar   return   his   message   nts   mes   

??????????????????ValidationResult???

public class ValidationResult {

    // ????????????????????????
    private boolean hasErrors = false;

    // ?????????????????????Map
    private Map<String, String> errorMsgMap = new HashMap<>();

    public boolean isHasErrors() {
        return hasErrors;
    }

    public void setHasErrors(boolean hasErrors) {
        this.hasErrors = hasErrors;
    }

    public Map<String, String> getErrorMsgMap() {
        return errorMsgMap;
    }

    public void setErrorMsgMap(Map<String, String> errorMsgMap) {
        this.errorMsgMap = errorMsgMap;
    }

    // ??????????????? ???????????????????????????????????? ?????????????????????Message??????
    public String getErrorMsg(){
        return StringUtils.join(errorMsgMap.values().toArray(), ",");
    }
}

??????????????????ValidatorImpl?????????

/**
 * InitializingBean?????????bean????????????????????????????????????????????????afterPropertiesSet???????????????????????????????????????????????????bean?????????????????????????????????
 * Spring??????????????????????????????ValidatorImpl???afterPropertiesSet()??????
 */
@Component
public class ValidatorImpl implements InitializingBean {

    private Validator validator;

    // ???????????????????????????????????????
    public ValidationResult validate(Object bean){
        ValidationResult validationResult = new ValidationResult();
        Set<ConstraintViolation<Object>> constraintViolationSet = validator.validate(bean);
        if(constraintViolationSet.size() > 0){
            // ??????0 ???????????????
            validationResult.setHasErrors(true);
            for (ConstraintViolation<Object> constraintViolation : constraintViolationSet) {
                String errorMsg = constraintViolation.getMessage();
                String propertyName = constraintViolation.getPropertyPath().toString();
                validationResult.getErrorMsgMap().put(propertyName, errorMsg);
            }
        }
        return validationResult;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // ???hibernate validator?????????????????????????????????????????????
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }
}

????????????????????????????????????

public class UserModel {

    private Integer id;

    @NotBlank(message = "name????????????")
    private String name;

    @NotNull(message = "??????????????????")
    private Byte gender;

    @NotNull(message = "??????????????????")
    @Min(value = 0, message = "??????????????????0")
    @Max(value = 150, message = "??????????????????150")
    private Integer age;

    @NotNull(message = "????????????????????????")
    @Size(min = 11, max = 11, message = "?????????????????????11???")
    private String telphone;

    @NotNull(message = "????????????????????????")
    private String registerMode;
    private String thirdPartyId;

    @NotNull(message = "??????????????????")
    private String encrptPassword;
    @Autowired
    private ValidatorImpl validator;

    @Transactional
    public void register(UserModel userModel) throws BusinessException {
        if(userModel == null){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        // 
        ValidationResult validationResult = validator.validate(userModel);
        if(validationResult.isHasErrors()){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR, validationResult.getErrorMsg());
        }

以上是关于SpringBoot ????????????????????????的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot入门到精通-SpringBoot自定义starter

SpringBoot.06.SpringBoot日志管理

SpringBoot.06.SpringBoot日志管理

最全面的SpringBoot教程——SpringBoot概述

SpringBoot入门到精通-SpringBoot集成SSM开发项目

如何把springboot插件删除干净