yaml和jsr303

yaml配置注入实体类:

  1. 在实体类上写上注解@Component和@ConfigurationProperties(prefix = “类名”)
  2. jsr303需要写注解@Validated
  3. 例如:
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
    @Email(message = "邮箱格式的问题")
    String name;
    @NotNull(message = "不能为blank")
    int age;
    Boolean happy;
    Date bir;
    Dog dog;
    Map<String,Object> map;
    List<Object> list;

    public Person() {
    }

4.yaml

server:
  port: 8081
  #通过yml给类赋值
person:
  name: 1191132442@qq.com
  age: 5
  happy: true
  bir: 2019/8/9
  map: {name: 1,sex: 5}
  list:
    - code
    - music
    - girl
    - lubao
  dog:
    name: ${person.address:santian}
    age: ${random.int}
dog:
  name: 旺财
  age: 7
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值