VS Code打開使用IDEA搭建的Spring Boot項目運行提示"snakeyaml was not found on the classpath"錯誤

今天用VS Code打開以前基於IDEA搭建並開發的Spring Boot項目,啓動調試後出現以下錯誤:java

17:43:05.214 [restartedMain] ERROR org.springframework.boot.SpringApplication - Application run failed

java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'

    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:538)

    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadForFileExtension(ConfigFileApplicationListener.java:497)

    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:465)

    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$null$6(ConfigFileApplicationListener.java:447)

    at java.lang.Iterable.forEach(Iterable.java:75)

    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$load$7(ConfigFileApplicationListener.java:446)

    at java.lang.Iterable.forEach(Iterable.java:75)

    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:443)

    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:335)

    at org.springframework.boot.context.config.ConfigFileApplicationListener.addPropertySources(ConfigFileApplicationListener.java:214)

    at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:197)

    at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:184)

    at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:170)

    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)

    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)

    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)

    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)

    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)

    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)

    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:358)

    at org.springframework.boot.SpringApplication.run(SpringApplication.java:317)

    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)

    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)

    at com.beyondbit.uffice.resource.ResourceApplication.main(ResourceApplication.java:13)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    at java.lang.reflect.Method.invoke(Method.java:498)

    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

Caused by: java.lang.IllegalStateException: Attempted to load applicationConfig: [classpath:/application.yml] but snakeyaml was not found on the classpath

    at org.springframework.boot.env.YamlPropertySourceLoader.load(YamlPropertySourceLoader.java:47)

    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadDocuments(ConfigFileApplicationListener.java:556)

    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:518)

    ... 28 common frames omitted

啓動時找到了application.yml文件,但沒有找到snakeyaml的jar。最終錯誤代碼來自於YamlPropertySourceLoader的load方法:spring

@Override
public List<PropertySource<?>> load(String name, Resource resource)
        throws IOException {
    if (!ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", null)) {
        throw new IllegalStateException("Attempted to load " + name
                + " but snakeyaml was not found on the classpath");
    }
    List<Map<String, Object>> loaded = new OriginTrackedYamlLoader(resource).load();
    if (loaded.isEmpty()) {
        return Collections.emptyList();
    }
    List<PropertySource<?>> propertySources = new ArrayList<>(loaded.size());
    for (int i = 0; i < loaded.size(); i++) {
        propertySources.add(new OriginTrackedMapPropertySource(
                name + (loaded.size() != 1 ? " (document #" + i + ")" : ""),
                loaded.get(i)));
    }
    return propertySources;
}

在IDEA查看項目依賴時候發現snakeyaml是運行時加載的,VS Code不會讀取IDEA的iml文件,因此引起了這個問題。app

因而打開pom.xml,手動添加對snakeyaml的依賴:ide

<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
</dependency>
相關文章
相關標籤/搜索