其他分享
首页 > 其他分享> > jsr303数据校验-@Vaildated

jsr303数据校验-@Vaildated

作者:互联网

pojo类

package com.example.wsh.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
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
@Data
@AllArgsConstructor
@NoArgsConstructor
/*
* @ConfigurationProperties(prefix = "person")的作用
* 将配置文件中每一个属性值,映射到这个组件中,
* 告诉springBoot本类中所有属性值与配置文件一一绑定,
* 参数prefix="person":将配置文件中person值一一对应
* 只有这个组件是容器中的组件才能用@ConfigurationProperties
* */
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
    @Email()
    private String name;
    private int age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> map;
    private List<Object> list;
    private Dog dog;
}

格式不一致时报错

Field error in object 'person' on field 'name': rejected value [wanShiHao]; codes [Email.person.name,Email.name,Email.java.lang.String,Email]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [person.name,name]; arguments []; default message [name],[Ljavax.validation.constraints.Pattern$Flag;@73ba6fe6,.*]; default message [不是一个合法的电子邮件地址]; origin class path resource [application.yaml] - 19:9

标签:name,jsr303,Vaildated,private,校验,person,springframework,import,Email
来源: https://www.cnblogs.com/wupupupu/p/16410650.html