SpringBoot数据校验功能的实现

网友投稿 233 2022-08-26


SpringBoot数据校验功能的实现

1.pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.6.3

com.yl

validation

0.0.1-SNAPSHOT

validation

Demo project for Spring Boot

11

org.springframework.boot

spring-boot-starter-validation

spring-boot-starter-web

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.6.3

com.yl

validation

0.0.1-SNAPSHOT

validation

Demo project for Spring Boot

11

org.springframework.boot

spring-boot-starter-validation

spring-boot-starter-web

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

2.实体类,注意:可以不用加ValidationMessages.properties文件,可以直接在message=""地方直接写上报错信息

package com.yl.validation.model;

import com.yl.validation.validation.ValidationGroup1;

import com.yl.validation.validation.ValidationGroup2;

import javax.validation.constraints.*;

import java.io.Serializable;

public class User implements Serializable {

private Integer id;

MaGxBUVVj //校验字符串长度

@Size(min = 3,max = 8,message = "{user.username.size}",groups = ValidationGroup1.class)

private String username;

//非空校验

@NotNull(message = "{user.password.notnull}")

private String password;

//数值范围校验

@DecimalMin(value = "15",message = "{user.age.min}",groups = {ValidationGroup1.class, ValidationGroup2.class})

@DecimalMax(value = "30",message = "{user.age.max}",groups = {ValidationGroup1.class, ValidationGroup2.class})

private Integer age;

//非空校验

@NotNull(message = "{user.email.notnull}")

//邮箱格式校验

@Email(message = "{user.email.pattern}")

private String email;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

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 Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

@Override

public String toString() {

return "User{" +

"id=" + id +

", username='" + username + '\'' +

", password='" + password + '\'' +

", age=" + age +

", email='" + email + '\'' +

'}';

}

}

3.controller

package com.yl.validation.controller;

import com.yl.validation.model.User;

import com.yl.validation.validation.ValidationGroup1;

import com.yl.validation.validation.ValidationGroup2;

import org.springframework.validation.BindingResult;

import org.springframework.validation.ObjectError;

import org.springframework.validation.annotation.Validated;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

public class UserController {

@PostMapping("/addUser")

public void addUser(@Validated(ValidationGroup2.class) User user, BindingResult result) {

if (result != null && result.hasErrors()) {

List allErrors = result.getAllErrors();

for (ObjectError objectError : allErrors) {

System.out.println(objectError.getDefaultMessage());

}

}

}

}

4.ValidationMessages.Properties,手动新建放在resources目录下

user.username.size=用户名的长度应为3到8

user.password.notnull=密码不能为空

user.age.min=年龄最小值为15

user.age.max=年龄最大值为30

user.email.notnull=邮箱不能为空

user.email.pattern=邮箱格式不正确

5.组一

package com.yl.validation.validation;

public interface ValidationGroup1 {

}

6.组二

package com.yl.validation.validation;

public interface ValidationGroup2 {

}

7.测试结果,由于分了组,发现只有年龄校验有效!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python从门到精通(七):网络-03-创建Rpc服务接口(python调用rpc的方法)
下一篇:Python从门到精通(七):网络-02-创建Rest服务接口(python调用rest接口)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~