英文:
consuming a map from configmap to springboot application
问题
I have added a map in my values.yaml file like below:
在我的values.yaml文件中,我添加了一个如下的映射:
defaultview:
journey:
ce5f164c-ae0f-11e7-9220-c7137b83fb6a: 45
abc: -1
pqr: 30
I read this property in configmap like below:
我在configmap中读取这个属性,如下所示:
defaultview.journey: {{ .Values.defaultview.journey }}
When I check configmap I see entry like below in cm:
当我检查configmap时,我在cm中看到如下条目:
defaultview.journey:
----
map[abc:-1 ce5f164c-ae0f-11e7-9220-c7137b83fb6a:45 pqr:30]
Java class is trying to bind this property into a Map<String,Integer> like below:
Java类尝试将这个属性绑定到一个Map<String,Integer>中,如下所示:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Data
@Component
@ConfigurationProperties(prefix = "defaultview")
public class JourneyViewConfig {
private Map<String, Integer> Journey = new HashMap<>();
}
I see error thrown while spring boot startup:
在Spring Boot启动时,我看到了错误抛出:
Failed to bind properties under 'defaultview.journey' to java.util.Map<java.lang.String, java.lang.Integer>:
Property: defaultview.journey
Value: "map[abc:-1 ce5f164c-ae0f-11e7-9220-c7137b83fb6a:45 pqr:30]"
Origin: "defaultview.journey" from property source "<cm name>"
Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.Integer>]
Is there a way to inject Map directly from configmap to spring boot?
是否有一种方法可以直接从configmap注入Map到Spring Boot中?
Edit 1:
I am able to read the values in map by adding this dirty code:
通过添加这个不太优雅的代码,我能够读取映射中的值:
private Map<String, Integer> parseConfigValue(String configValue) {
Map<String, Integer> resultMap = new HashMap<>();
String trimmedInput = configValue.trim();
if (trimmedInput.startsWith("map[")) {
trimmedInput = trimmedInput.substring(4, trimmedInput.length() - 1);
String[] pairs = trimmedInput.split(" ");
for (String pair : pairs) {
String[] keyValue = pair.split(":");
if (keyValue.length == 2) {
String key = keyValue[0].trim();
Integer value = Integer.parseInt(keyValue[1].trim());
resultMap.put(key, value);
}
}
}
Is there a more subtle way of doing this?
是否有更加巧妙的方法来做这个?
英文:
I have added a map in my values.yaml file like below
defaultview:
journey:
ce5f164c-ae0f-11e7-9220-c7137b83fb6a: 45
abc: -1
pqr: 30
I read this property in configmap like below
defaultview.journey: {{ .Values.defaultview.journey }}
When I check configmap I see entry like below in cm
defaultview.journey:
----
map[abc:-1 ce5f164c-ae0f-11e7-9220-c7137b83fb6a:45 pqr:30]
Java class is trying to bind this property into a Map<String,Integer> like below
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Data
@Component
@ConfigurationProperties(prefix = "defaultview")
public class JourneyViewConfig {
private Map<String, Integer> Journey = new HashMap<>();
}
I see error thrown while spring boot startup
Failed to bind properties under 'defaultview.journey' to java.util.Map<java.lang.String, java.lang.Integer>:\\n\\n Property: defaultview.journey\\n Value: \\\"map[abc:-1 ce5f164c-ae0f-11e7-9220-c7137b83fb6a:45 pqr:30]\\\"\\n Origin: \\\"defaultview.journey\\\" from property source \\\"<cm name>\\\"\\n Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.Integer>
Is there a way to inject Map directly from configmap to spring boot?
Edit 1:
I am able to read the values in map by adding this dirty code
private Map<String, Integer> parseConfigValue(String configValue) {
Map<String, Integer> resultMap = new HashMap<>();
String trimmedInput = configValue.trim();
if (trimmedInput.startsWith("map[")) {
trimmedInput = trimmedInput.substring(4, trimmedInput.length() - 1);
String[] pairs = trimmedInput.split(" ");
for (String pair : pairs) {
String[] keyValue = pair.split(":");
if (keyValue.length == 2) {
String key = keyValue[0].trim();
Integer value = Integer.parseInt(keyValue[1].trim());
resultMap.put(key, value);
}
}
Is there a more subtle way of doing this?
答案1
得分: 1
这一行存在问题:
defaultview.journey: {{ .Values.defaultview.journey }}
你试图将一个结构化变量包含为一个简单字符串,因此导致了一个看起来像这样的 ConfigMap:
data:
defaultview.journey_broken: map[abc:-1 ce5f164c-ae0f-11e7-9220-c7137b83fb6a:45 pqr:30]
这不是有用的,因为它既不是有效的 YAML,也不是 JSON,而且可能不是你的应用程序所期望的格式。
你需要使用 toYaml
函数将你的结构化变量渲染为一个 YAML 文本块。例如:
data:
defaultview.journey: |
{{ indent 4 (.Values.defaultview.journey | toYaml) }}
这将产生:
data:
defaultview.journey: |
abc: -1
ce5f164c-ae0f-11e7-9220-c7137b83fb6a: 45
pqr: 30
英文:
This line is problematic:
defaultview.journey: {{ .Values.defaultview.journey }}
You're trying to include a structured variable as a simple string, so this is resulting in a ConfigMap that looks like:
data:
defaultview.journey_broken: map[abc:-1 ce5f164c-ae0f-11e7-9220-c7137b83fb6a:45 pqr:30]
That's not useful, as it's neither valid YAML nor JSON and probably isn't what your application is expecting.
You're going to need to use the toYaml
function to render your structured variable as a block of YAML text. For example:
data:
defaultview.journey: |
{{ indent 4 (.Values.defaultview.journey | toYaml) }}
Which results in:
data:
defaultview.journey: |
abc: -1
ce5f164c-ae0f-11e7-9220-c7137b83fb6a: 45
pqr: 30
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论