返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot数据校验功能的实现
  • 699
分享到

SpringBoot数据校验功能的实现

2024-04-02 19:04:59 699人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="Http://Maven.apac

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="Http://Maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>validation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>validation</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
            <artifactId>spring-boot-starter-WEB</artifactId>

            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

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;
    //校验字符串长度
    @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<ObjectError> 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.测试结果,由于分了组,发现只有年龄校验有效!

在这里插入图片描述

在这里插入图片描述

到此这篇关于SpringBoot数据校验的文章就介绍到这了,更多相关SpringBoot数据校验内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot数据校验功能的实现

本文链接: https://lsjlt.com/news/139928.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作