How to read your own YAML file (*****. Yml) in Java

Environment you are using

Tomcat:9 JDK:OpenJDK8(OPENJ9) spring-boot:2.0.5

Created process (probably an evil but moving one)

This is the configuration file you want to read ↓ ↓ ↓

[Project root]/src/main/resources/test.yml


value:
  item1:
    aaa: 12345
    bbb: 22345
  item2:
    dog: poti
    cat: tama

Java description is this ↓ ↓ ↓

xxxxx.java


DefaultResourceLoader resourceLoader;
InputStreamReader reader;

try {
	resourceLoader = new DefaultResourceLoader();
	Resource resource = resourceLoader.getResource("classpath:test.yml");
	reader = new InputStreamReader(resource.getInputStream());

    Yaml yaml = new Yaml();
    map = (Map<String, Object>) yaml.load(reader);
    
    System.out.println(ValueMap.toString());

} catch (Exception e) {
	e.printStackTrace();
}

I thought I should use ResourceLoader, and initially

@Autowired
ResourceLoader resourceLoader;

I tried the declaration, but it failed with NullPointerException, probably because the Autowired annotation is not working. So, I got it by using the DefaultResourceLoader class, which is a wicked or brute force method.

Created process (the one that didn't work after all)

`* This process worked in eclipse in the development environment, but when I fixed it in War and placed it in Tomcat, it stopped working probably because the directory structure changed. ``

This is the configuration file you want to read ↓ ↓ ↓

[Project root]/src/main/resources/test.yml


value:
  item1:
    aaa: 12345
    bbb: 22345
  item2:
    dog: poti
    cat: tama

Java description is this ↓ ↓ ↓

xxxxx.java


InputStreamReader reader;

try {
	reader = new InputStreamReader(ClassLoader.getSystemResourceAsStream("test.yml"));

	Yaml yaml = new Yaml();
	Map<String, String> ValueMap = (Map<String, String>) yaml.load(reader);

	System.out.println(ValueMap.toString());

} catch (Exception e) {
	e.printStackTrace();
}

The contents set in ValueMap look like this ↓ ↓ ↓ ValueMapの内容.png

I was able to get it like that.

Add dependency

I was able to get it in the above form, so when I thought "Which war should I use?"

Recommended Posts

How to read your own YAML file (*****. Yml) in Java
[Ruby] How to convert CSV file to Yaml (Yml)
How to read log4j configuration file in Java project summarized in jar file Memo
How to create your own annotation in Java and get the value
How to convert a file to a byte array in Java
Contemplation: How to take advantage of functional interfaces in your own functions (java)
Read Java properties file in C #
[Forge] How to register your own Entity and Entity Render in 1.13.2
How to learn JAVA in 7 days
Handle your own annotations in Java
Log output to file in Java
How to use classes in Java?
How to name variables in Java
How to concatenate strings in java
Sample to read and write LibreOffice Calc fods file in Java 2021
How to get the length of an audio file in java
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
Understand java interface in your own way
How to add jar file in ScalaIDE
How to do base conversion in Java
How to implement coding conventions in Java
How to embed Janus Graph in Java
Read xlsx file in Java with Selenium
How to get the date in java
Sample to unzip gz file in Java
Read a string in a PDF file with Java
How to display a web page in Java
How to get Class from Element in Java
How to hide null fields in response in Java
[Java] Read the file in src / main / resources
How to bind to property file in Spring Boot
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
How to write Java String # getBytes in Kotlin?
How to ZIP a JAVA CSV file and manage it in a Byte array
Read JSON in Java
Read Java Property file
<java> Read Zip file and convert directly to string
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
[Java] How to omit the private constructor in Lombok
How to jump from Eclipse Java to a SQL file
How to input / output IBM mainframe files in Java?
[Java] How to extract the file name from the path
Java memory management and how to read GC Viewer
How to create a data URI (base64) in Java
How to generate / verify ID token in Java Memo
How to test file upload screen in Spring + Selenium
How to convert A to a and a to A using AND and OR in Java
How to Git manage Java EE projects in Eclipse
How to debug the generated jar file in Eclipse
Summary of how to implement default arguments in Java
Gzip-compress byte array in Java and output to file
How to put old Java (8 series) in macOS 10.15 Catalina
Notes on how to use regular expressions in Java
How to save a file with the specified extension under the directory specified in Java to the list
How to get the class name / method name running in Java
How to correctly check the local HTML file in the browser