gpt4 book ai didi

java - 无法使用@ConfigurationProperties 将 map 与spring boot yaml 文件中的复杂键绑定(bind)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:50:09 25 4
gpt4 key购买 nike

我正在尝试使用 spring boot 和 @ConfigurationProperties 注释将具有复杂键的映射从 spring yaml 配置文件解码到 java.util.Map。有很多关于带有简单键的 map 的例子,比如

map: 
key: value

甚至用简单的键和复杂的值映射,比如

map:
key: {firstPartOfComplexValue: alpha, secondPartOfComplexValue: beta}

我已经测试了上述两个示例 - 效果很好。

现在我需要一个映射中的复杂键:

map:
? {firstPartOfAKey: someValue1, secondPartOfAKey: someValue2}: value

这种解码的结果是一个空映射。你能告诉我我做错了什么吗提前致谢

这是我的代码:

application.yml

custom:
users:
? {firstPartOfAKey: hello, secondPartOfAKey: world} : tom

要解码的 bean

@Component
@ConfigurationProperties("custom")
public class MyBean {
private Map<Key, String> users = new HashMap<>();

public Map<Key, String> getUsers() {
return users;
}

public void setUsers(Map<Key, String> users) {
this.users = users;
}

@Override
public String toString() {
return users.toString();
}

public static class Key {
private String firstPartOfAKey;
private String secondPartOfAKey;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
return Objects.equals(firstPartOfAKey, key.firstPartOfAKey) &&
Objects.equals(secondPartOfAKey, key.secondPartOfAKey);
}

@Override
public int hashCode() {
return Objects.hash(firstPartOfAKey, secondPartOfAKey);
}

public String getFirstPartOfAKey() {
return firstPartOfAKey;
}

public void setFirstPartOfAKey(String firstPartOfAKey) {
this.firstPartOfAKey = firstPartOfAKey;
}

public String getSecondPartOfAKey() {
return secondPartOfAKey;
}

public void setSecondPartOfAKey(String secondPartOfAKey) {
this.secondPartOfAKey = secondPartOfAKey;
}

@Override
public String toString() {
return String.format("firsPartOfKey: '%s', secondPartOfKey: '%s'", firstPartOfAKey, secondPartOfAKey);
}
}
}

java 配置(它是空的)

@Configuration
@ComponentScan(basePackages = {"com"})
@EnableAutoConfiguration
@EnableConfigurationProperties
public class Config {
}

单元测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Config.class})
public class TestProps {

@Autowired
private MyBean myBean;

@Test
public void testYamlPropsLoad() {
System.out.println(myBean);
}
}

测试仅为具有复杂键的 map 打印“{}”。其他 map (带有简单的键)运行良好。

最佳答案

如果你的测试没有任何异常地执行,你的 MyBean 的绑定(bind)似乎根本没有被处理,通常应该有这样的异常:

Binding to target {} failed:

Property: custom.null Value: {{firstPartOfAKey=hello, secondPartOfAKey=world}=tom} Reason: Failed to convert property value of type 'java.lang.String' to required type 'com.example.MyBean$Key' for property 'null'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.example.MyBean$Key': no matching editors or conversion strategy found

显然,解析器无法处理复杂的键,并且正在解释键 null 和值 {firstPartOfAKey=hello, secondPartOfAKey=world}=tom

也许您可以通过实现 custom editor / converter 找到另一种处理配置的方法.

关于java - 无法使用@ConfigurationProperties 将 map 与spring boot yaml 文件中的复杂键绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44548461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com