YAML with Spring & Java Q&As

Q1. What is YAML?
A1. YAML Ain’t Markup Language. It is a human friendly data serialization standard for all programming languages. A YAML file has an extension of “.yml“.

Q2. What are the 3 rules of YAML?

Rule 1: Indentation YAML uses a fixed indentation scheme to represent relationships between data layers (i.e. 2 spaces). Never use a tab.

Rule 2: Colons & case sensitive All keys/properties are case-sensitive. (“Hello”, is not the same as “hello”) and key value pairs are separated by “:” followed by a space.

Keys can be nested.

Rule 3: Dashes To represent lists of items, a single dash followed by a space is used. Multiple items are a part of the same list as a function of their having the same level of indentation.

Q3. Where will you use YAML in Java application?

1) Configuring applications: Java developers mostly deal with property (.properties), XML, and JSON files for configuring applications. YAML file can be used to configure Java applications without being too verbose as XML and being more expressive than .properties and JSON formats.

All you need is the YAML library

Java application.yml file example

The properties can be accessed via Spring configs as

Can you spot the expressive nature of the YAML file?

2) Serialize & Deserialize Java objects: YamlBeans makes it easy to serialize and deserialize Java object graphs. YAML is a human-friendly data format. Replace XML and properties files with YAML for more expressive power.

3) Spring boot supports YAML E.g.application.yml file.

More application configuration examples



Java Interview FAQs

800+ Java Interview Q&As