SnakeYaml jar 存在于 classPath:snakeyaml-1.26.jar
2330 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.NoSuchMethodError: org.yaml.snakeyaml.Yaml.<init>(Lorg/yaml/snakeyaml/constructor/BaseConstructor;Lorg/yaml/snakeyaml/representer/Representer;Lorg/yaml/snakeyaml/DumperOptions;Lorg/yaml/snakeyaml/LoaderOptions;Lorg/yaml/snakeyaml/resolver/Resolver;)V
at org.springframework.boot.env.OriginTrackedYamlLoader.createYaml(OriginTrackedYamlLoader.java:71)
at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:162)
at org.springframework.boot.env.OriginTrackedYamlLoader.load(OriginTrackedYamlLoader.java:76)
at org.springframework.boot.env.YamlPropertySourceLoader.load(YamlPropertySourceLoader.java:50)
at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadDocuments(ConfigFileApplicationListener.java:607)
我遇到了类似的问题,我的解决方案是在与 Spring Boot完全相同的版本中使用 Snakeyaml。
一般来说,一个好技巧是从
org.springframework.boot:spring-boot-dependencies
导入 Maven 依赖项,以避免版本不兼容。
如果您使用
mvn
,请将其添加到 pom 中的 <dependencyManagement>
下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
然后在
<dependencies>
下:
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<scope>runtime</scope>
</dependency>
例如 spring-boot-dependency-2.3.6.RELEASE.pom 在1.26中使用snakeyaml:
<snakeyaml.version>1.26</snakeyaml.version>
而 spring-boot-dependency-2.5.12.pom 使用 1.28:
<snakeyaml.version>1.28</snakeyaml.version>
spring-boot-dependency-2.6.1.pom 使用 1.29:
<snakeyaml.version>1.29</snakeyaml.version>
希望这有帮助。
当我将snakeyaml从1.33更新到2.0时,我收到了这个错误。 您需要将版本降级到1.33
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.33</version>
</dependency>
mvn 存储库链接:https://mvnrepository.com/artifact/org.yaml/snakeyaml
2.15.0
解决了我的问题。
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
if (resource == null) {
return new PropertiesPropertySource(name, new Properties());
}
Yaml yaml = new Yaml();
Map<String, Object> loadedYaml = yaml.load(resource.getInputStream());
// Convert all values to String (mimicking the old behavior)
Properties properties = new Properties();
convertToStringProperties(loadedYaml, properties, "");
return new PropertiesPropertySource(name, properties);
}
/**
* Recursively converts all properties to String.
*/
private void convertToStringProperties(Map<String, Object> sourceMap, Properties properties, String parentKey) {
for (Map.Entry<String, Object> entry : sourceMap.entrySet()) {
String key = (parentKey.isEmpty()) ? entry.getKey() : parentKey + "." + entry.getKey();
Object value = entry.getValue();
if (value instanceof Map) {
// Recursively handle nested properties
convertToStringProperties((Map<String, Object>) value, properties, key);
} else {
// Convert value to String (mimicking old behavior)
properties.put(key, value != null ? value.toString() : null);
}
}
}
}