本文共 4670 字,大约阅读时间需要 15 分钟。
//实体类package com.zkdj.urlCheck.spring_boot_1.main.java.model;import org.hibernate.validator.constraints.Length; import javax.validation.constraints.*; import java.util.Date;public class User{ @NotBlank(message = "用户名不能为空") private String username; private String password; private String confirmPassword; @NotBlank(message = "用户昵称不能为空") @Length(max = 12, message = "用户昵称不能超过12位") private String nickname; @Length(max = 12, message = "用户真实姓名不能超过12位") private String realname; @Pattern(regexp = "^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\\d{8})$", message = "手机号格式不正确") private String mobile; @Email private String email; @Min(value = 0, message = "性别选择不正确")// 0已下 @Max(value = 2, message = "性别选择不正确")// 2以上 private Integer sex; 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; } }
//controllerpackage com.zkdj.urlCheck.spring_boot_1.main.java.controller.blank;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.validation.Valid;import org.springframework.validation.BindingResult;import org.springframework.validation.FieldError;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import com.zkdj.urlCheck.spring_boot_1.main.java.model.User;import com.zkdj.urlCheck.spring_boot_1.main.java.utils.JSONResult;@RestController@RequestMapping("/api/blank")public class BlankController { @PostMapping("update") public JSONResult update( @Valid User user, BindingResult result, HttpServletRequest request, HttpServletResponse response) { if (result.hasErrors()){ MaperrorsMap = getErrors(result); return JSONResult.errorMap(errorsMap); } // TODO 后续要改,增加令牌token,会整合进redis,分布式会话 return JSONResult.ok(); } public Map getErrors(BindingResult result){ Map map=new HashMap<>(); List fieldErrorsList = result.getFieldErrors(); for (FieldError error:fieldErrorsList){ // 错误的属性 String errorField = error.getField(); //错误的提示 String errorMsg = error.getDefaultMessage(); map.put(errorField,errorMsg); } return map; }}
//返回参数类package com.zkdj.urlCheck.spring_boot_1.main.java.utils;import java.util.Date;public class JSONResult { /** * 数据 */ private Object data; /** * 消息提示 */ private String msg; /** * 状态码 */ private int status; public Object getData() { return data; } public void setData(Object data) { this.data = data; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public JSONResult(int i, String string, Object data) { this.data=data; this.status=i; this.msg=string; } public static JSONResult errorMap(Object data) { return new JSONResult(501, "error", data); } public static JSONResult ok() { return new JSONResult(200, "success", "good"); } public static void main(String[] args) { Date date = new Date(); System.out.println(date); }}
转载地址:http://jyrqj.baihongyu.com/