JSR303校验

JSR303校验_第1张图片

person:
  name: kuangshen${random.uuid}
  age: ${random.int}
  happy: false
  hello: nihaoa
  bitth: 2023/07/23
  maps: {k1: v1,k2: v2}
  lists:
    - sing
    - dance
    - girl
  dog:
    name: ${person.hello:hello1213}_旺财
    age: 19

dog:
    first-name: ${person.hello:hello1213}_旺财1
    age: 19

 

package com.kuang.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
//@Component 把这个类添加到spring组件里相当于在application里创建了一个这个类的bean
@ConfigurationProperties(prefix = "dog")
public class Dog {

    private String firstName;

    private Integer age;

    public Dog() {
    }

    public Dog(String name, Integer age) {
        this.firstName = name;
        this.age = age;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public Integer getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Dog{" +
                "firstName='" + firstName + '\'' +
                ", age=" + age +
                '}';
    }
}

松散绑定

JSR303校验_第2张图片

需要导入一个依赖


        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        

 

JSR303数据校验

package com.kuang.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
//加载指定配置文件
//javaConfig 绑定我们配置文件的值,可以采取这些方式!
//@PropertySource(value = "classpath:qingjiang.properties")
@Validated
public class Person {
    //spEL表达式取值 ${key}
   // @Value("${name}")
   @Email(message = "邮箱格式错误")
    private String name;
    private Integer age;
    private Boolean happy;
    private Date bitth;
    private Map maps;
    private List lists;
    private Dog dog;

    public Person() {
    }

    public Person(String name, Integer age, Boolean happy, Date bitth, Map maps, List lists, Dog dog) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.bitth = bitth;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

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

    public Boolean getHappy() {
        return happy;
    }

    public void setHappy(Boolean happy) {
        this.happy = happy;
    }

    public Date getBitth() {
        return bitth;
    }

    public void setBitth(Date bitth) {
        this.bitth = bitth;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", bitth=" + bitth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}
 
  

验证新版本springboot需要在导入一个依赖


        
            org.springframework.boot
            spring-boot-starter-validation
        

你可能感兴趣的:(SpringBoot,java,开发语言,spring,boot)