Spring boot application.json properties example

In this article, we will show you how to use JSON instead of the properties file or the yml file in Spring Boot by custom configuration.

Before moving to the example first you must be familiar with the following components.

1.spring.factories file

The file is located in src/java/resources/META-INF/spring.factories. The spring.factories file is a file that spring automatically loads when booting up. It contains references to many configuration classes.

For example,
org.springframework.boot.env.PropertySourceLoader=com.example.demo.JsonPropertySourceLoader
Here we registered our custom Json property source loader


2. YamlPropertySourceLoader

Load the resource into one or more property sources. Implementations may either return a list containing a single source or in the case of a multi-document format such as YAML a source for each document in the resource. Refer
import org.springframework.boot.env.YamlPropertySourceLoader;

public class JsonPropertySourceLoader
extends YamlPropertySourceLoader {
@Override
public String[] getFileExtensions() {
return new String[] { "json" };
}
}
getFileExtensions() returns the file extensions that the loader supports.Refer


3. @Value

@Value annotation provides a convenient way to inject property values into components.
@Value("${testdata1}")
private String testData1;

@Value("#{${valuesMap}}")
private Map<String, Integer> valuesMap;


Spring boot application.json properties demo

Project Directory



JsonPropertySourceLoader

package com.example.demo;

import org.springframework.boot.env.YamlPropertySourceLoader;

public class JsonPropertySourceLoader
extends YamlPropertySourceLoader {
@Override
public String[] getFileExtensions() {
return new String[] { "json" };
}
}


spring.factories

org.springframework.boot.env.PropertySourceLoader=com.example.demo.JsonPropertySourceLoader


application.json

{
"server.port":9080,
"testdata1":"testing",
"valuesMap":"{key1: '1', key2: '2', key3: '3'}"
}


DemoApplication.java

package com.example.demo;

import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication {

@Value("${testdata1}")
private String testData1;

@Value("#{${valuesMap}}")
private Map<String, Integer> valuesMap;

@GetMapping
public String echo() {

System.out.println("Iterating the map....");
for (Map.Entry<String, Integer> entry : valuesMap.entrySet()) {
System.out.println("Key = " + entry.getKey()
+ ", Value = " + entry.getValue());
}
return testData1;
}

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}



Run, Test and Verify the application

Step1: Download or clone the source code to a local machine. - Click here


Step2: mvn clean install


Step3: Run the Spring Boot application - mvn spring-boot:run


Step4: Access the URL - http://localhost:9080/


Step5: Verify the output

Download the source code

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - Blowfish Encryption and decryption Example

Java - DES Encryption and Decryption example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String

Top 5 Java ORM tools - 2024