SpringBoot - yame语法规则 && 读取yml数据案例测试

yaml语法规则

yaml、yml文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5FRBg7ob-1651326026108)(springboot.assets/image-20220430195714568.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HcPfEdP6-1651326026109)(springboot.assets/image-20220430200442682.png)]

请添加图片描述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IohKE4qW-1651326026111)(springboot.assets/image-20220430202358378.png)]

字面值表示方式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bKU2JjAZ-1651326026112)(springboot.assets/image-20220430203037817.png)]

数组表示方式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MvJpJtwS-1651326026112)(springboot.assets/image-20220430203135139.png)]

核心规则

数据前面加空格与冒号隔开

总结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cU7cnEc3-1651326026113)(springboot.assets/image-20220430203205632.png)]

读取yml数据->@Value

编写yml文件

country: china
provice: beijing
city: beijing
area: haidian

port: 8080

party: true


birthday: 1949-10-01

user:
  name: liuliu
  age: 16

user2:
  name: honghong
  age: 18

a:
  b:
    c:
      d: 123

likes: [game,music]

users:
  - name: zhangsan
    age: 18
  - name: lisi
    age: 17

server:
  port: 80

读取单一的数据

package com.taotao.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/4/28 16:39
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {

    //读取yaml数据中的单一数据
    @Value("${country}")
    private String country1;

    @GetMapping
    public String getById(){
        System.out.println("spring boot 2 is running");
        System.out.println("country1====>" + country1);
        return "spring boot is running";


    }
}

读取成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Rp389ZUs-1651326026114)(springboot.assets/image-20220430204555487.png)]

读取二级数据

package com.taotao.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/4/28 16:39
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {

    //读取二级数据
    @Value("${user2.name}")
    private String name2;

    @Value("${user.name}")
    private String name;

    @GetMapping
    public String getById(){
        System.out.println("spring boot 2 is running");
        System.out.println("name1====>" + name2);
        System.out.println("name====>" + name);
        return "spring boot is running";
    }
}

读取成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FodV11Vd-1651326026114)(springboot.assets/image-20220430212514677.png)]

读取数组数据

package com.taotao.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/4/28 16:39
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {

    //读取二级数据
    @Value("${likes[0]}")
    private String likes0;

    @Value("${likes[1]}")
    private String likes1;

    @GetMapping
    public String getById(){
        System.out.println("spring boot 2 is running");
        System.out.println("name1====>" + likes0);
        System.out.println("name====>" + likes1);
        return "spring boot is running";
    }
}

读取成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Tao1dfKp-1651326026115)(springboot.assets/image-20220430212923461.png)]

读取对象属性

package com.taotao.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/4/28 16:39
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {

    @Value("${users[0].name}")
    private String name;

    @Value("${users[0].age}")
    private String age;

    @GetMapping
    public String getById(){
        System.out.println("spring boot 2 is running");
        System.out.println("name1====>" + name);
        System.out.println("name====>" + age);
        return "spring boot is running";
    }
}

读取成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CLdCEU8z-1651326026116)(springboot.assets/image-20220430213500786.png)]

读取服务器端口

package com.taotao.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/4/28 16:39
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {

    @Value("${server.port}")
    private String port;

    @GetMapping
    public String getById(){
        System.out.println("spring boot 2 is running");
        System.out.println("port====>" + port);
        return "spring boot is running";
    }
}

读取成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IS7GPRuK-1651326026116)(springboot.assets/image-20220430213830778.png)]

注意

8080端口是默认端口,读取不到

小结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7rSurIjz-1651326026116)(springboot.assets/image-20220430213926802.png)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值