SpringBoot-yaml语法规则和读取数据

本文阅读 3 分钟
首页 代码,Java 正文

img ✨博客主页:👉 不会压弯的小飞侠 ✨欢迎关注:👉点赞🎀收藏⭐留言✒ ✨系列专栏:👉 SpringBoot专栏(每日更新) ✨如果觉得博主的文章还不错的话,请三连支持一下博主。 ✨欢迎大佬指正,一起学习!一起加油! img

YAML 是 “YAML Ain’t Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。

  • YAML是一种数据序列化格式。
  • yaml扩展名
    - .yaml - .yml(主流)
  • 大小写敏感
  • 属性层次关系使用多行描述,每行结尾使用冒号结束
  • 使用缩进表示层级关系,同层左侧对齐,只允许使用空格(不允许使用Tab键)
  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
  • 表示注释

⭐字面值表示方式:

boolean: true
float: 3.14
int: 15
#表示空
null: ~
string: xiaofeixia
date: 2022-7-9
#日期与时间用T连接
datetime: 2022-7-9T12:00:30+02:00

⭐数组表示方式:

likes:
  - music
  - draw
  - game

likes1: [music,draw,game]

⭐对象数组格式:

user2:
  - name: xiaofeixia
    age: 22
  - name: xiaomage
    age: 26

user3:
  -
    name: xiaofeixia
    age: 22
  -
    name: xiaomage
    age: 27

⭐对象数组缩略格式:

user4: [{ name:xiaofeixia,age:21},{ name:xiaofeixia,age:22}]
  • 使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名}

⭐编写yaml文件

server:
  port: 81
country: china
province: henan
city: zhengzhou
area: shangqiu

party: true
birthday: 2022-11-11

user8:
  name: xiaofeixia
  age: 22
user1:
  name: xiaofeixia
  age: 22

a:
  B:
    C:
      d:
        e: abc

likes:
  - music
  - draw
  - game

likes1: [music,draw,game]

user2:
  - name: xiaofeixia
    age: 22
  - name: xiaomage
    age: 26

user3:
  -
    name: xiaofeixia
    age: 22
  -
    name: xiaomage
    age: 27

user4: [{ name:xiaofeixia,age:21},{ name:xiaofeixia,age:22}]

⭐读取单一数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController { 
    //读取yaml数据中的单一数据
    @Value("${country}")
    public String country1;

    @GetMapping
    public String ById(){ 
        System.out.println("springboot is running...");
        System.out.println("country=="+country1);  //country==china 
        return "springboot is running...";
    }
}

⭐读取二级数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController { 
    @Value("${user8.name}")
    public String username;
    @GetMapping
    public String ById(){ 
        System.out.println("springboot is running...");       
        System.out.println("username=="+username);         //username==xiaofeixia 
        return "springboot is running...";
    }
}

⭐读取数组数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController { 
 @Value("${likes[0]}")
    public String likes1;
     @GetMapping
    public String ById(){ 
        System.out.println("springboot is running...");       
        System.out.println("likes1=="+likes1);  //likes1==music 
        return "springboot is running...";
    }
   
}

⭐读取服务器端口号

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController { 
   @Value("${server.port}")
    public String port;
    @GetMapping
    public String ById(){ 
        System.out.println("springboot is running...");       
        System.out.println("port=="+port);  //port==81 
        return "springboot is running...";
    }
}

⭐读取对象属性

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController { 
    @Value("${user2[0].age}")
    public String age2;
    @GetMapping
    public String ById(){ 
        System.out.println("springboot is running...");       
         System.out.println("age2=="+age2);  //age2==22 
        return "springboot is running...";
    }
}

⭐封装全部数据到Environment对象

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController { 
    @Autowired
    private Environment env;
    @GetMapping
    public String ById(){         
        System.out.println(env.getProperty("server.port"));
        System.out.println(env.getProperty("user8.name"));        
        return "springboot is running...";
    }
}

⭐读取yaml引用类型属性数据

⭐⭐application.yml

server:
  port: 81
#创建类用于封装下面的数据
#由spring去加载数据到对象中,一定要告诉spring加载这组信息
#使用的时候直接从spring中获取信息
datasource:
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/springboot
  username: root
  password: root

⭐⭐MyDataSource

自定义对象封装指定数据 1.定义数据模型封装yaml文件中对应的数据

package com.jkj;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//2.定义spring的管控Bean
@Component
//3.指定加载数据
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource { 
    private String driver;
    private String url;
    private String username;
    private String password;

    public String getDriver() { 
        return driver;
    }

    public void setDriver(String driver) { 
        this.driver = driver;
    }

    public String getUrl() { 
        return url;
    }

    public void setUrl(String url) { 
        this.url = url;
    }

    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;
    }

    @Override
    public String toString() { 
        return "MyDataSource{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

⭐⭐读取数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController { 
    @Autowired
    private MyDataSource myDataSource;
    @GetMapping
    public String ById(){      
        System.out.println(myDataSource);
        //MyDataSource{driver='com.mysql.jdbc.Driver', url='jdbc:mysql://localhost/springboot', username='root', password='root'}
        return "springboot is running...";
    }
}

⭐变量的引用

⭐⭐application.yml

server:
  port: 81
baseDir: E:\window
tempDir: ${ baseDir}\temp

⭐⭐读取数据

package com.jkj.controller;
import com.jkj.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/yamlBooks")
public class BookController { 
     @Value("${tempDir}")
     public String temp;   
    @GetMapping
    public String ById(){      
         System.out.println("temp=="+temp);  //temp==E:\window\temp
        return "springboot is running...";
    }
}

⭐ context-path

只写:

server:
  port: 81

控制台输出:path为空 img 加上context-path后:

server:
  port: 81
  servlet:
    context-path: /test

控制台输出页面: img 注意:在浏览器输入:http://localhost:81/test/yamlBooks 进行测试。

  1. file
  2. setting
  3. 搜索Spring Core
  4. 如下图所示将Severity修改为Warning img
本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/qq_43514330/article/details/125690460
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复