Simple SpringBoot and YAML understanding

1. Create a maven project; (jar)

2. Import spring boot related dependencies

<parent>

        <groupId>org.springframework.boot</groupId>
        <artifactId>spring‐boot‐starter‐parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring‐boot‐starter‐web</artifactId>
        </dependency>

    </dependencies>

3. Write a main program; start the Spring Boot application

/**
 * @SpringBootApplication to mark a main program class, indicating that this is a Spring Boot application
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        // Spring application starts
        SpringApplication.run(HelloWorldMainApplication .class,args);
    }

}

4. Write related Controller and Service

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!";
    }

}

1. Know YAML

  YAML is a markup language similar to XML and JSON. YAML emphasizes data centricity, not markup language centricity. Therefore, the definition of YAML itself is relatively simple, known as "a humanized data format language".

1.1 Design goals of YAML:
  • easy for humans to read

  • Can be used for data exchange between different programs

  • Suitable for describing data structures used by programs, especially scripting languages

  • Rich expressiveness and extensibility

  • easy to use

1.2 YAML given XML, JSON
  • YAML given XML

    • Has the same advantages as XML, but is simpler, agile, etc.
  • YAML given JSON

    • JSON can be seen as a subset of YAML, meaning that what JSON can do, YAML can also do

    • YAML can be expressed simpler and easier to read than JSON, eg "strings don't need quotes". So YAML can easily be written in JSON format, but this is not recommended

    • YAML can describe more complex structures than JSON, for example "relationship anchors" can represent data references (such as references to duplicate data).

1.3 YAML organizational structure

  A YAML file can consist of one or more documents (that is, relatively independent organizational structures), using "---" (three horizontal lines) between documents as a delimiter at the beginning of each document. At the same time, the document can also use "..." (three dots) as a terminator (optional). As shown below:

  • For a single document, the separator "---" can be omitted.

  • Each document does not need to use the terminator "..." to indicate the end, but for network transmission or streaming, it is beneficial to software processing as a clear end symbol. (e.g. to know the end of the document without knowing the stream is closed)

  YAML considers data to consist of the following three structures: (each document consists of a mix of three structures)

  • scalar (equivalent to data type)

  • Sequences (equivalent to arrays and lists)

  • Key-value table (equivalent to Map table)

2. YAML writing specification

  • Specification 1: The document uses Unicode encoding as the character standard encoding, such as UTF-8

  • Specification 2: Use "#" to indicate the content of comments

    # 客户订单
    date: 2015-02-01 customer:  - name: Jai items:  - no: 1234 # 订单号  - descript: cpu 
  • Specification Three: Use whitespace as a nested indentation tool. Two spaces are generally recommended for indentation, tabs are deprecated (not even supported)

  • Specification Four: Sequence Representation

    • Use "-" (horizontal line) + a single space for a single list item
      --- # 文档开始 - 第一章 简介 - 第二章 设计目录 
    • Use "[]" to represent a set of data
      --- # 文档开始 [blue, red, green] 
    • Combination representation. Each structure can be nested to form complex presentation structures.
      --- # 文档开始 - [blue, red, green] # 列表项本身也是一个列表 - [Age, Bag] - site: {osc:www.oschina.net, baidu: www.baidu.com} # 这里是同 键值表 组合表示
  • Specification 5: Key-value table

    • Use ":" (colon) + space for a single key-value pair
      # 客户订单
      date: 2015-02-01 customer:  - name: Jai items:  - no: 1234 # 订单号  - descript: cpu  - price: ¥800.00 
    • 使用"{}"表示一个键值表
      # 客户订单
      date: 2015-02-01 customer:  - name: Jai items: {no: 1234, descript: cpu, price: ¥800.00} 
    • "? " 问号+空格表示复杂的键。当键是一个列表或键值表时,就需要使用本符号来标记。
       # 使用一个列表作为键
       ? [blue, reg, green]: Color
       # 等价于
       ? - blue
         - reg
         - gree
       : Color
      
    • 组合表示。每个结构都可以嵌套组成复杂的表示结构。 

       Color:
          - blue
          - red - green # 相当于 (也是 JSON 的表示) {Color: [blue, red, green]} div: - border: {color: red, width: 2px} - background: {color: green} - padding: [0, 10px, 0, 10px] # 使用缩进表示的键值表与列表项 items: - item: cpu model: i3 price: ¥800.00 - item: HD model: WD price: ¥450.00 # 上面使用 “-” 前导与缩进来表示多个列表项,相当于下面的JSON表示 items: [{item:cpu, model:i3, price:¥800.00}, {item:HD, model:WD, price: ¥450.00}] 
  • 规范六:文本块

    • 使用 “|” 和文本内容缩进表示的块:保留块中已有的回车换行。相当于段落块

      yaml: |      # 注意 ":" 与 "|" 之间的空格
         JSON的语法其实是YAML的子集,大部分的JSON文件都可以被YAML的解释器解释。
         如果你想更深入的学习YAML,我建议你去 http://www.yaml.org 看看 
    • 使用 “>” 和文本内容缩进表示的块:将块中回车替换为空格,最终连接成一行。

      yaml: >      # 注意 ":" 与 ">" 之间的空格,另外可以使用空行来分段落
         JSON的语法其实是YAML的子集,
         大部分的JSON文件都可以被YAML的解释器解释。
      
         如果你想更深入的学习YAML,我建议你去 http://www.yaml.org 看看 
    • 使用定界符“”(双引号)、‘’(单引号)或回车表示的块:最终表示成一行。

      yaml:     # 使用回车的多行,最终连接成一行。
         JSON的语法其实是YAML的子集,
         大部分的JSON文件都可以被YAML的解释器解释。
      
      yaml:     # 使用了双引号,双引号的好处是可以转义,即在里面可以使用特殊符号
         "JSON的语法其实是YAML的子集, 大部分的JSON文件都可以被YAML的解释器解释。" 
  • 规范七:数据类型的约定

    • 对一些常用数据类型的表示格式进行了约定,包括:

       integer: 12345     # 整数标准形式
       octal: 0o34 # 八进制表示,第二个是字母 o  hex: 0xFF # 十六进制表示  float: 1.23e+3 # 浮点数  fixed: 13.67 # 固定小数  minmin: -.inf # 表示负无穷  notNumber: .NaN # 无效数字  null: # 空值  boolean: [true, false] # 布尔值  string: ‘12345‘ # 字符串  date: 2015-08-23 # 日期  datetime: 2015-08-23T02:02:00.1z # 日期时间  iso8601: 2015-08-23t21:59:43.10-05:00 # iso8601 日期格式  spaced: 2015-08-23 21:59:43.10 -5 # ? 
    • “!”(叹号)显式指示类型,或自定义类型标识。单叹号通常是自定义类型,双叹号是内置类型

       isString: !!str 2015-08-23 # 强调是字符串不是日期数据  picture: !!binary | # Base64 图片 R0lGODlhDAAMAIQAAP//9/X 17unp5WZmZgAAAOfn515eXv Pz7Y6OjuDg4J+fn5OTk6enp 56enmleECcgggoBADs= #下面是内置类型 !!int # 整数类型 !!float # 浮点类型 !!bool # 布尔类型 !!str # 字符串类型 !!binary # 也是字符串类型 !!timestamp # 日期时间类型 !!null # 空值 !!set # 集合 !!omap, !!pairs # 键值列表或对象列表 !!seq # 序列,也是列表 !!map # 键值表 #下面是一些例子:  --- !!omap  - Mark: 65  - Sammy: 63  - Key: 58  --- !!set # 注意,“?”表示键为列表,在这里列表为 null ? Mark ? Sammy ? Key # 下面是自定义的类型或标识 %TAG ! tag:clarkevans.com,2002: # % 是指令符号  --- !shape # Use the ! handle for presenting # tag:clarkevans.com,2002:circle  - !circle  center: &ORIGIN {x: 73, y: 129}  radius: 7  - !line  start: *ORIGIN  finish: { x: 89, y: 102 }  - !label  start: *ORIGIN  color: 0xFFEEBB  text: Pretty vector drawing. 
  • 规范八:锚点与引用,定义数据的复用。

    • 第一步:使用 “&” 定义数据锚点(即要复制的数据)
    • 第二步:使用 “*” 引用上述锚点数据(即数据的复制目的地)
       ---
       hr:
         - Mark McGwire
         # Following node labeled SS
         - &SS Sammy Sosa # 定义要复制的数据 rbi: - *SS # Subsequent occurrence 这里是数据复制目标 - Ken Griffey 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324605339&siteId=291194637