Spring Boot - Reading custom object as value in map from yml file - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Saturday, September 29, 2018

Spring Boot - Reading custom object as value in map from yml file

In this Spring Boot post, You will understand how to read the map from application.yml file where the value of the map is user-defined class.

You may also interested in how to read a Map from the properties file.

P.S Tested with spring boot 2.0.5.RELEASE

Properties file

#Empty file
error-code-meaning:
  errorDescriptionMapping:
    1000: {errorCode: 1000, meaning: 'AUTHENTICATION_ERROR', httpStatusCode: 401}
    1001: {errorCode: 1001, meaning: 'AUTHORIZATION_ERROR', httpStatusCode: 403}
    1002: {errorCode: 1002, meaning: 'RESPONSE_TIMEOUT', httpStatusCode: 500}
    1003: {errorCode: 1003, meaning: 'NETWORK_ERROR', httpStatusCode: 500}
    1004: {errorCode: 1004, meaning: 'UNKNOWN_ERROR', httpStatusCode: 500}

Project dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.walking.techie</groupId>
    <artifactId>custom-object-value-map-read-from-yml-file</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>custom-object-value-map-read-from-yml-file</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

ErrorCodeConfig

This is a configuration class which will read the application.yml file and store key-value pair in Map.

package com.walking.techie.config;

import com.walking.techie.model.ErrorDetails;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "error-code-meaning")
@PropertySource("application.yml")
public class ErrorCodeConfig {
  private Map<String, ErrorDetails> errorDescriptionMapping;

  public Map<String, ErrorDetails> getErrorDescriptionMapping() {
    return errorDescriptionMapping;
  }

  public void setErrorDescriptionMapping(Map<String, ErrorDetails> errorDescriptionMapping) {
    this.errorDescriptionMapping = errorDescriptionMapping;
  }
}

ErrorDetails model

package com.walking.techie.model;

public class ErrorDetails {
  private String errorCode;
  private String meaning;
  private Integer httpStatusCode;

  public String getErrorCode() {
    return errorCode;
  }

  public void setErrorCode(String errorCode) {
    this.errorCode = errorCode;
  }

  public String getMeaning() {
    return meaning;
  }

  public void setMeaning(String meaning) {
    this.meaning = meaning;
  }

  public Integer getHttpStatusCode() {
    return httpStatusCode;
  }

  public void setHttpStatusCode(Integer httpStatusCode) {
    this.httpStatusCode = httpStatusCode;
  }

  @Override
  public String toString() {
    return "ErrorDetails{"
        + "errorCode='"
        + errorCode
        + '\''
        + ", meaning='"
        + meaning
        + '\''
        + ", httpStatusCode="
        + httpStatusCode
        + '}';
  }
}

Run Application

package com.walking.techie;

import com.walking.techie.config.ErrorCodeConfig;
import com.walking.techie.model.ErrorDetails;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.util.Map;

@SpringBootApplication
public class Application {

  public static void main(String[] args) {
    ApplicationContext applicationContext = SpringApplication.run(Application.class, args);
    ErrorCodeConfig propertiesFileBean = applicationContext.getBean(ErrorCodeConfig.class);
    Map<String, ErrorDetails> keyValuePair = propertiesFileBean.getErrorDescriptionMapping();
    for (String key : keyValuePair.keySet()) {
      System.out.println("Key:: " + key + " value:: " + keyValuePair.get(key));
    }
  }
}

Output

When you run this standalone application you will see something like below:

2018-09-29 17:43:39.842  INFO 18928 --- [           main] com.walking.techie.Application           : Started Application in 1.636 seconds (JVM running for 2.56)
Key:: 1000 value:: ErrorDetails{errorCode='1000', meaning='AUTHENTICATION_ERROR', httpStatusCode=401}
Key:: 1001 value:: ErrorDetails{errorCode='1001', meaning='AUTHORIZATION_ERROR', httpStatusCode=403}
Key:: 1002 value:: ErrorDetails{errorCode='1002', meaning='RESPONSE_TIMEOUT', httpStatusCode=500}
Key:: 1003 value:: ErrorDetails{errorCode='1003', meaning='NETWORK_ERROR', httpStatusCode=500}
Key:: 1004 value:: ErrorDetails{errorCode='1004', meaning='UNKNOWN_ERROR', httpStatusCode=500}

find the above working code from git.

No comments :

Post a Comment