使用hibernate校驗欄位

寫給吳諾的部落格發表於2020-12-11

只需要引入start-web框架就可以使用hibernate校驗欄位,就無需在後臺寫程式碼校驗

public class CenterUserBo {

    @ApiModelProperty(value="使用者名稱", name="username", example="json", required = false)
    private String username;
    @ApiModelProperty(value="密碼", name="password", example="123456", required = false)
    private String password;
    @ApiModelProperty(value="確認密碼", name="confirmPassword", example="123456", required = false)
    private String confirmPassword;


    @NotBlank(message = "使用者暱稱不能為空")
    @Length(max = 12, message = "使用者暱稱不能超過12位")
    @ApiModelProperty(value="使用者暱稱", name="nickname", example="傑森", required = false)
    private String nickname;

    @Length(max = 12, message = "使用者真實姓名不能超過12位")
    @ApiModelProperty(value="真實姓名", name="realname", example="傑森", required = false)
    private String realname;

    @Pattern(regexp = "^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\\d{8})$", message = "手機號格式不正確")
    @ApiModelProperty(value="手機號", name="mobile", example="13999999999", required = false)
    private String mobile;

    @Email
    @ApiModelProperty(value="郵箱地址", name="email", example="imooc@imooc.com", required = false)
    private String email;

    @Min(value = 0, message = "性別選擇不正確")
    @Max(value = 2, message = "性別選擇不正確")
    @ApiModelProperty(value="性別", name="sex", example="0:女 1:男 2:保密", required = false)
    private Integer sex;
    @ApiModelProperty(value="生日", name="birthday", example="1900-01-01", required = false)
    private Date birthday;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "CenterUserBO{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", confirmPassword='" + confirmPassword + '\'' +
                ", nickname='" + nickname + '\'' +
                ", realname='" + realname + '\'' +
                ", mobile='" + mobile + '\'' +
                ", email='" + email + '\'' +
                ", sex=" + sex +
                ", birthday=" + birthday +
                '}';
    }
}

    @ApiOperation(value = "修改使用者資訊",notes = "修改使用者資訊",httpMethod = "POST")
    @PostMapping("update")
    public IMOOCJSONResult update(
            @ApiParam(name = "userId",value = "使用者id",required = true)
            @RequestParam String userId,
            @RequestBody CenterUserBo @Valid centerUserBo,
            BindingResult result,
            HttpServletRequest request,
            HttpServletResponse response){
        //判斷BindingResult 是否有錯誤的驗證資訊,有,return
        if(result.hasErrors()){
            Map<String, Object> errorMap = getErrors(result);
            return IMOOCJSONResult.errorMap(errorMap);
        }
        Users userResult = centerUserService.updateUserInfo(userId, centerUserBo);
        userResult = setNullProperty(userResult);
        CookieUtils.setCookie(request,response,"user",
                JsonUtils.objectToJson(userResult),true);
        //TODO 後續會修改 增加令牌token,整合redis,分散式會話
        return IMOOCJSONResult.ok();
    }
    private Map<String,Object> getErrors(BindingResult result){
        Map<String,Object> map = new HashMap<>();
        List<FieldError> fieldErrors = result.getFieldErrors();
        for(FieldError fieldError : fieldErrors ){
            String errorField = fieldError.getField();
            String errorMsg = fieldError.getDefaultMessage();
            map.put(errorField, errorMsg);
        }
        return map;
    }

 

相關文章