Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use YAML syntax in SpringBoot and what points should be paid attention to?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use YAML grammar in SpringBoot and what points we should pay attention to". In daily operation, I believe many people have doubts about how to use YAML grammar in SpringBoot and what points they should pay attention to. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use YAML grammar in SpringBoot and what points you should pay attention to"! Next, please follow the editor to study!

YAML Grammar and some points for attention

The yaml suffix name can be .yml or .yaml

One thing to note about syntax is that it is regularly indented to control its hierarchical relationship, and its key: value form must have a space in the middle.

The entity class (setter,getter,toString omitted) is @ Component@ConfigurationProperties (prefix = "person") public class Person {private String lastName; private Integer age; private List hobbies; private Map map; private Dog dog;}

Idea will warn you after using the @ ConfigurationProperties annotation, but there is no prompt function when you configure it in yml, so click on the warning, and you will find that the web page may not be found. In this case, you just need to lower the version of the URL bar, as shown below.

Put the contents of the box into pom, then rerun the project, and you will have code hints for your own classes marked @ ConfigurationProperties in yaml.

Public class Dog {private String name; private Integer age;} application.yml configuration file to inject values into person: last-name: Zhang San age: 25 hobbies:-Basketball-running-Guitar map: {K1: Hello, K2: world} dog: name: dog age: 10

The list can be in the form above or below. If you see clearly that there is a space in the cursor, the object type can be {}, such as Map below.

Loose binding of yaml

LastName hump nomenclature in Java can be expressed in other forms in yaml. Such as

Last_namelast-namelastNameLASTNAME

Play this for the first time if you want to test in the test class, it is possible that you will not be able to run stuck in Maven. Inside the box, the solution: my mistakes collect notes

Simple introduction to YAML Grammar

You can check the official or other blog materials, there will be a lot of content.

The design of YAML language refers to languages such as JSON,XML and SDL. YAML emphasizes data-centric, concise and easy to read and easy to write.

Interesting naming:

AML is a recursive acronym for "YAML Ain't a Markup Language" (YAML is not a markup language). When the language was developed, YAML actually meant "Yet Another Markup Language" (still a markup language).

Grammatical features

Case sensitive

Express hierarchical relationships by indentation

Prohibit the use of tab indentation, only use the spacebar (personally feel that this is the most important)

The number of indented spaces is not important, as long as the same level is left-aligned.

Use # for comments

Supported data structures

Object: a collection of key-value pairs, also known as mapping / hashes / dictionary (dictionary)

Array: a group of values arranged in order, also known as sequence / list

Scalar (scalars): a single, non-separable value

The distinction between double quotes and single quotation marks

Double quotation marks "": the special characters in the string are not escaped, and the special characters are used as the meaning they want to express.

Yamlname: "123\ n123"

Output: 123 line wrap 123

If you do not add quotation marks, special characters will be escaped and treated as strings.

The way to write the value

1. String

Use "or" or not use quotation marks

Yamlvalue0: 'hello Worldworkers value1: "hello World!" value2: hello World!

two。 Boolean value

Represented by true or false.

3. Figures

Yaml12 # integer 014 # octal integer 0xC # hexadecimal integer 13.4 # floating point number 1.2e+34 # index .inf null # infinity

4. Null value

Null or ~ represents

5. Date

Use the iso-8601 standard to represent dates

Yamldate: 2018-01-01t16:59:43.10-05:00

The time format of yaml file in springboot: date: yyyy/MM/dd HH:mm:ss

6. Cast (understand)

YAML allows the use of an exclamation point!, casting data types, single exclamation points are usually custom types, and double exclamation points are built-in types.

Yamlmoney:!! str123date:! Booleantrue

Built-in type list

Set # integer type!! float # floating point type!! bool # Boolean type!! str # string type!! binary # is also a string type! timestamp # date time type!! null # null value!! set # collection! OMAPP # empty pairs # list or object list! seq # sequence, also list!! map # key value table

7. Object (key point)

The form of Map (attribute and value) (key-value pair): key: (space) v: indicates a bunch of key-value pairs, and spaces cannot be omitted.

Yamlcar: color: red brand: BMW

One-line writing

Yamlcar: {color: red,brand: BMW}

Equivalent to JSON format:

Json {"color": "red", "brand": "BMW"}

8. Array

A set of lines at the beginning of a hyphenation line to form an array.

Yamlbrand:-audi-bmw-ferrari

One-line writing

Yamlbrand: [audi,bmw,ferrari]

Equivalent to JSON

Json ["auri", "bmw", "ferrari"]

9. Text block

| |: use the | indented block of annotated text content, and you can retain the carriage return and line feeds that already exist in the block. |

Yamlvalue: | hello world!

Output: hello newline world!

+ means to keep the newline at the end of the text block, and-to delete the newline at the end of the string.

Yamlvalue: | hellovalue: |-hellovalue: | + hello

Output: hello\ n hello hello\ n\ n (as many carriage returns as there are\ n)

Note that there must be a new line between "|" and the text.

Use the > annotated text content to indent the block, replace the carriage return in the block with a space, and finally join it into a line

Yamlvalue: > helloworld!

Output: hello space world!

Note the space between ">" and the text

10. Anchor points and references

Use & define the data anchor (that is, the data to be copied) and use * to reference the anchor data (that is, the replication destination of the data)

Yamlname: & a yamlbook: * abooks:-java-* a-python

Output book: yaml

Output books: [java,yaml,python]

Note * references cannot be appended.

At this point, the study of "how to use YAML grammar in SpringBoot and what points we should pay attention to" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report