0

I am trying to load come properties from config using the Spring @Configuration & @ConfigurationProperties combination. I have a POJO that extends HashMap<Integer, String> and has a single variable inside. (See MyConfigPojo below). From the .yaml file, I have the same shape. However, when booting up the app, I get an error when trying to parse the string for the defaultValue into an Integer.

@Configuration
@ConfigurationPropeties(prefix = "my-config")
public class MyConfigPojo extends HashMap<Integer, String> {

  private String defaultValue;

  private String getValueForIdOrDefault(int id); // this.get(id) OR ELSE defaultValue
}

In config I have this:

myConfig:
  1: "my first value"
  23: "my 23rd value"
  defaultValue: "cheese"

Which results in a

APPLICATION FAILED TO START
Description:
Failed to bind properties under 'myPackage' to MyConfigPojo:
Property: myConfig[1]
Value: cheese

I thought that was weird, so I turned on TRACE logs and found this:

99 common frames omittedCaused by: java.lang.NumberFormatException: For input string: "defaultValue" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

Is there a way to intercept reading this file or to tell Spring how to set the values properly?

C Taylor
  • 117
  • 1
  • 9
  • What are trying to do? jus want to read the properties from yaml to map right? – Dinesh Dontha Sep 01 '20 at 17:23
  • Basically I will have a list of these Int -> String maps each with its own default value that I'd like to be read from config. e.g.:`[{1: "firstMapValueForKey1", 4: "fistMapValaueForKey4", defaultValue: "defaultValueForFirstMap"}, {2: "secondMapValueForKey2", 9: "secondMapValueForKey9", defaultValue: "secondMapDefaultValue"}]` – C Taylor Sep 01 '20 at 17:25
  • Your actual problem is using trying to read a map value a: b(key: value), so you have to keep the key type consistent, initially you are using integer, But defaultValue is string, so you are getting NumberFormatException – Dinesh Dontha Sep 01 '20 at 17:33
  • I know that I can just change the type of the Map and would get rid of the problem. I am trying to see if there's a way not to do it that way. e.g. with @ConfigurationPropetiesBinding – C Taylor Sep 01 '20 at 17:46
  • Oh great, you did not mention anything about this annotation @ConfigurationPropetiesBinding in question. Anyway, goodluck – Dinesh Dontha Sep 01 '20 at 17:48

1 Answers1

0

You are trying to read a Map from .yml file, for that, you need @ConfigurationProperties annotation enough, don't need @Configuration annotation(if using spring boot). If you are using spring-framework, you need @Configuration also.

Use this example:

myConfig:
  map:
    1: "my first value"
    23: "my 23rd value"
    defaultValue: "cheese"

MyConfigPojo.java

package com.org;


import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.HashMap;

@Configuration
@ConfigurationProperties(prefix = "my-config")
public class MyConfigPojo {

    private HashMap<String, String> map;

    public HashMap<String, String> getMap() {
        return map;
    }

    public void setMap(HashMap<String, String> map) {
        this.map = map;
    }
}

Now, you can Autowire this MyConfigPojo class anywhere(for instance, in controller class) and read those map keys and values.

@Autowired
MyConfigPojo pojo;

Now, you have considered keys & Values as String datatype for that Map, you will not get NumberFormatException.

Dinesh Dontha
  • 537
  • 5
  • 17
  • Good to know about `@Configuration`. I know I can switch the Map from `` to ``, but I am trying to find a way around that. e.g. using some form of @ConfigurationPropertiesBinding – C Taylor Sep 01 '20 at 17:47
  • When I removed the `@Configuration` annotation, I started receiving this error `Field myPojo in mypackage required a bean of type 'myPackage.MyPojo' that could not be found. – C Taylor Sep 01 '20 at 18:25
  • Yeah, if you are using spring boot, you dont need it. In Spring, you need it. – Dinesh Dontha Sep 01 '20 at 18:30