It is possible to map the data given in the spring application yaml/json/properties file with the java class directly. This process, known as mapping, allows to directly link the data in the configuration files to corresponding fields in Java classes. With this. it is easy the update the data directly in the properties file without need to build the application again.
Example 1: Mapping payment purposes which are available in different languages.
package org.example.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "transaction.category-details")
@Data
public class TransactionCategories {
private List<Category> categories;
@Data
public static class Category {
private String name;
private String code;
private List<Subcategory> subcategories;
}
@Data
public static class Subcategory {
private String name;
private String code;
}
}
Example 3: Map the key value pairs of message templates
Define the properties in application.yaml file.
messaging:
templates:
welcome: "Welcome to our messaging app! We're glad to have you on board."
invitation: "You've been invited to join a group chat. Click the link to join."
notification: "You have a new message. Click here to view it."