Mapping Properties to Java Class

About

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.

Mapping payment purposes which are available in different languages.

Define the properties in application.yaml file.

payment:
  purpose:
    purposeEn:
      - 'Others'
      - 'Holidays'
    purposeAr:
                                                                      'آحرون' - 
                                                                     'العطل' -      
    purposeFr:
      - 'Autres'
      - 'Vacances'

Create PaymentPurposes.java class

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 = "payment.purpose")
@Data
public class PaymentPurposes {
    private List<String> purposeEn;
    private List<String> purposeAr;
    private List<String> purposeFr;
}

Map the parent-child hierarchy property of transaction category.

Define the properties in application.yaml file.

Create TransactionCategories.java class

Map the key value pairs of message templates

Define the properties in application.yaml file.

Create MessagingTemplates.java class

Add Integration Tests to test above 3 examples.

Output

Make sure to include below dependencies and failsafe plugin to test

Mapping card types and fees in a Map.

Define the properties in application.yaml file.

Create CardProperties.java class

Mapping Error Codes and Description

Define the properties in application.yaml file.

Create ErrorConfig.java class

Mapping Audit Categories

Define the properties in application.yaml file.

Create AuditConfig.java class

Last updated