基于springBoot的参数校验

定义restApi接口时,需要对请求参数进行校验,为了避免大量的if...else..语句,导致代码太过臃肿

 

pom依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
/**
* 基于springBoot的参数校验
*
* @author nanfengxiangbei
* @date 2022-06-08
*/
@RestController
@RequestMapping(value = "/demo")
public class ValidateDemoController {
private static final Logger logger = LoggerFactory.getLogger(ValidateDemoController.class);

/**
* http://127.0.0.1:10092/demo/validate 请求样例
*
* {
* "userName":"nanfengxiangbei",
* "phone":"13688881122",
* "email":"1021608888@qq.com",
* "age":18,
* "workAge":10,
* "url":"http://www.baidu.com",
* "birthDate":"2022-06-08 00:00:00"
* }
*
*/
@RequestMapping(value = "/validate")
@ResponseBody
public BaseResponseVO<UserParamsVO> getMobileByAccount(@Validated @RequestBody UserParamsVO userParamsVO,
BindingResult result) {

List<FieldError> fieldErrors = result.getFieldErrors();
if (!fieldErrors.isEmpty()) {
return BaseResponseVO.fail("-1", fieldErrors.get(0).getDefaultMessage());
}

return BaseResponseVO.success("0", "SUCCESS", new UserParamsVO("username", "13688887777"));
}
}

/**
* 请求参数VO
* @author nanfengxiangbei
* @date 2022/6/8 10:12
*/
public class UserParamsVO {

@NotEmpty(message = "用户名不能为空")
private String userName;

@NotNull(message = "手机号不能为空")
@Length(max = 11, message = "手机号超过11位")
private String phone;

@Email(message = "邮箱格式不对")
private String email;

@Min(value = 18, message = "未满十八岁")
private Integer age;

@Max(value = 50, message = "工龄输入超过合法值")
private Long workAge;

@URL(message = "url格式不正确")
private String url;

@Pattern(regexp = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$", message = "时间格式校验不通过")
private String birthDate;

public UserParamsVO() {
}

public UserParamsVO(String userName, String phone) {
this.userName = userName;
this.phone = phone;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getEmail() {
return email;
}

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

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public Long getWorkAge() {
return workAge;
}

public void setWorkAge(Long workAge) {
this.workAge = workAge;
}

public String getBirthDate() {
return birthDate;
}

public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

@Override
public String toString() {
return "UserParamsVO{" +
"userName='" + userName + '\'' +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
", age=" + age +
", workAge=" + workAge +
", birthDate=" + birthDate +
", url='" + url + '\'' +
'}';
}
}


posted @ 2022-06-10 18:03  南风向北  阅读(56)  评论(0编辑  收藏  举报