Spring Boot官方例子《Developing Your First Spring Boot Application》无法运行

2023-11-08

官方的第一个例子就卡住了:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#getting-started.first-application
按照要求,一步一步走:

  1. 查看Java版本和MVN版本:
$ java -version
openjdk version "17.0.4.1" 2022-08-12 LTS
OpenJDK Runtime Environment (build 17.0.4.1+1-LTS)
OpenJDK 64-Bit Server VM (build 17.0.4.1+1-LTS, mixed mode, sharing)
$ mvn -v
Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: usr/Users/developer/tools/maven/3.8.5
Java version: 17.0.4.1, vendor: BellSoft, runtime: /Users/developer/sdkman/candidates/java/17.0.4.1-librca
  1. 创建POM文件(我放在了D:\dev\2023\000000_learn\230310_spring\pom.xml):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.4</version>
    </parent>

    <!-- Additional lines to be added here... -->
</project>
  1. 添加类依赖,在pom.xml的<!-- Additional lines to be added here... -->下面添加:
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
  1. 创建源码文件:src/main/java/MyApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class MyApplication {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
  1. 运行程序:
$ mvn spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v3.0.4)
....... . . .
....... . . . (log output here)
....... . . .
........ Started MyApplication in 0.906 seconds (process running for 6.514)

多么美好,但是我的运行结果是:

D:\dev\2023\000000_learn\230310_spring>mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:myproject >------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:3.0.4:run (default-cli) > test-compile @ myproject >>>
[INFO]
[INFO] --- maven-resources-plugin:3.3.0:resources (default-resources) @ myproject ---
[INFO] skip non existing resourceDirectory D:\dev\2023\000000_learn\230310_spring\src\main\resources
[INFO] skip non existing resourceDirectory D:\dev\2023\000000_learn\230310_spring\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ myproject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\dev\2023\000000_learn\230310_spring\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.3.0:testResources (default-testResources) @ myproject ---
[INFO] skip non existing resourceDirectory D:\dev\2023\000000_learn\230310_spring\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ myproject ---
[INFO] No sources to compile
[INFO]
[INFO] <<< spring-boot-maven-plugin:3.0.4:run (default-cli) < test-compile @ myproject <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:3.0.4:run (default-cli) @ myproject ---
[INFO] Attaching agents: []

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.4)

2023-03-10T20:21:20.631+08:00  INFO 21024 --- [           main] MyApplication                            : Starting MyApplication using Java 17.0.2 with PID 21024 (D:\dev\2023\000000_learn\230310_spring\target\classes started by jianmin.liu in D:\dev\2023\000000_learn\230310_spring)
2023-03-10T20:21:20.631+08:00  INFO 21024 --- [           main] MyApplication                            : No active profile set, falling back to 1 default profile: "default"
2023-03-10T20:21:20.678+08:00  WARN 21024 --- [           main] ionWarningsApplicationContextInitializer :

** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.


2023-03-10T20:21:20.983+08:00  WARN 21024 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:D:\app\apache-maven-3.8.6\alimvn\org\springframework\boot\spring-boot-autoconfigure\3.0.4\spring-boot-autoconfigure-3.0.4.jar!/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$PoolConfiguration.class]
2023-03-10T20:21:20.983+08:00  INFO 21024 --- [           main] .s.b.a.l.ConditionEvaluationReportLogger :

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-03-10T20:21:20.999+08:00 ERROR 21024 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:D:\app\apache-maven-3.8.6\alimvn\org\springframework\boot\spring-boot-autoconfigure\3.0.4\spring-boot-autoconfigure-3.0.4.jar!/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$PoolConfiguration.class]
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:463) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:317) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:276) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:128) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:289) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:243) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:196) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:164) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:398) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:283) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:344) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:115) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:747) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:565) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[spring-boot-3.0.4.jar:3.0.4]
        at MyApplication.main(MyApplication.java:15) ~[classes/:na]
Caused by: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
        at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:54) ~[spring-boot-autoconfigure-3.0.4.jar:3.0.4]
        at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:88) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:71) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isConditionMatch(ClassPathScanningCandidateComponentProvider.java:518) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isCandidateComponent(ClassPathScanningCandidateComponentProvider.java:501) ~[spring-context-6.0.6.jar:6.0.6]
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:436) ~[spring-context-6.0.6.jar:6.0.6]
        ... 20 common frames omitted
Caused by: java.lang.NoClassDefFoundError: io/r2dbc/spi/ValidationDepth
        at java.base/java.lang.Class.getDeclaredFields0(Native Method) ~[na:na]
        at java.base/java.lang.Class.privateGetDeclaredFields(Class.java:3297) ~[na:na]
        at java.base/java.lang.Class.getDeclaredField(Class.java:2608) ~[na:na]
        at org.springframework.boot.context.properties.bind.DefaultBindConstructorProvider$Constructors.isInnerClass(DefaultBindConstructorProvider.java:147) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.DefaultBindConstructorProvider$Constructors.getCandidateConstructors(DefaultBindConstructorProvider.java:137) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.DefaultBindConstructorProvider$Constructors.getConstructors(DefaultBindConstructorProvider.java:105) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.DefaultBindConstructorProvider.getBindConstructor(DefaultBindConstructorProvider.java:44) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.ValueObjectBinder$ValueObject.get(ValueObjectBinder.java:190) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.ValueObjectBinder.bind(ValueObjectBinder.java:67) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.Binder.lambda$bindDataObject$5(Binder.java:476) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.Binder$Context.withIncreasedDepth(Binder.java:590) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.Binder$Context.withDataObject(Binder.java:576) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.Binder.bindDataObject(Binder.java:474) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.Binder.bindObject(Binder.java:414) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:343) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:332) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:262) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:223) ~[spring-boot-3.0.4.jar:3.0.4]
        at org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PooledConnectionFactoryCondition.getMatchOutcome(ConnectionFactoryConfigurations.java:139) ~[spring-boot-autoconfigure-3.0.4.jar:3.0.4]
        at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-3.0.4.jar:3.0.4]
        ... 26 common frames omitted
Caused by: java.lang.ClassNotFoundException: io.r2dbc.spi.ValidationDepth
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na]
        ... 46 common frames omitted

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.108 s
[INFO] Finished at: 2023-03-10T20:21:21+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.0.4:run (default-cli) on project myproject: Process terminated with exit code: 1 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

查了很多都说是因为JAVA版本不对,但实际上是缺少对package的声明。
只要在src/main/java/MyApplication.java的第一行加上:

package com.example.myproject;

就解决了。

搞笑的是,官方文档也提出了,在写程序的时候“最好”不要使用default package,但是他却没有按照这个规范做。。

6.2.1. Using the “default” Package
When a class does not include a package declaration, it is considered to be in the “default package”. The use of the “default package” is generally discouraged and should be avoided. It can cause particular problems for Spring Boot applications that use the @ComponentScan, @ConfigurationPropertiesScan, @EntityScan, or @SpringBootApplication annotations, since every class from every jar is read.

We recommend that you follow Java’s recommended package naming conventions and use a reversed domain name (for example, com.example.project).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Boot官方例子《Developing Your First Spring Boot Application》无法运行 的相关文章

随机推荐

  • Java教程:Mybatis一对多查询,并定义ResultMap

    Java教程 Mybatis一对多查询 并定义ResultMap 源码 PO 一方 ApiModel 事故管理 public class OcAccidentPO implements Serializable 事故ID ApiModelP
  • 1. MongoDB快速实战与基本原理

    分布式技术MongoDB 1 MongoDB介绍 1 1 什么是MongoDB 1 2 MongoDB vs 关系型数据库 1 3 MongoDB的技术优势 1 4 MongoDB的应用场景 2 MongoDB快速开始 2 1 linux安
  • 基于Python的在线自主评测系统设计与实现

    博主介绍 擅长Java 微信小程序 Python Android等 专注于Java技术领域和毕业项目实战 文末获取源码联系 精彩专栏推荐订阅 不然下次找不到哟 Java项目精品实战案例 300套 Java微信小程序项目实战 200套 Pyt
  • antd的TreeSelect获取父节点的值

    antd中有个treeSelect树选择组件 数据结构是一个树形结构 当我们点击时候会打开 然后可以选择不同的节点 选中的时候 当前的节点会被回填上输入框 但是现在有个需求是想选择子节点的时候 回填的时候 父节点跟子节点一起回填上 中间加个
  • Java全排列算法练习

    题目 素数就是不能再进行等分的数 比如 2 3 5 7 112 3 5 7 11 等 9 3 39 3 3 说明它可以3等分 因而不是素数 我们国家在 19491949 年建国 如果只给你 1 9 4 91 9 4 9 这 44 个数字卡片
  • 电脑文件&软件搬家迁移十大工具

    10 大适用于 Windows 的数据迁移软件 数据迁移至关重要 几乎所有组织都依赖于此 如果您认为数据传输不是一件容易的事 那么数据迁移软件可以帮上忙 1 奇客电脑迁移 将现有操作系统 软件 文件迁移到 新电脑的最佳方法之一是使用名为奇客
  • Unity---委托与事件

    目录 1 委托和事件在使用上的区别是什么 2 delegate委托 2 1示意图 2 2 DelegetTest cs 2 3 Deleget A cs 2 4 Deleget B cs 2 5 运行unity 点击按键 A 2 6 点击按
  • 《k8s-1.13版本源码分析》- 调度器设计

    本文原始地址 https farmer hutao github io k8s source code analysis core scheduler desigh html github项目地址 https github com farm
  • docker 安装elasticsearch以及kibana

    1 安装elasticsearch 1 1 拉取镜像 执行下面的命令将es的镜像拉取到本地 docker pull docker elastic co elasticsearch elasticsearch 6 5 0 1 2 启动容器 没
  • ➹使用webpack配置多页面应用(MPA)

    使用webpack配置MPA 为什么需要使用 webpack 构建多页应用呢 因为某些项目使用 SPA 不太合适 大多是 SEO 的原因 或者您在做项目时有其他的需求 如果你有如下需求 使用 ES6 进行开发 期望使用面向对象开发 clas
  • java 文件备注_JAVA 文档注释

    JAVA文档注释 一JAVA注释类型 Java注释分为三类 1单行注释 2多行注释 3文档注释 单行注释多行注释 主要用于代码辅助性的说明便于理解代码的逻辑 文档注释 主要用生成API文档 二文档注释类型 文档注释紧挨类方法属性前面放置否则
  • Kyligence Zen产品体验——一站式指标平台泰酷辣~

    文章目录 一 前言 二 为什么需要指标化平台 三 什么是Kyligence Zen 四 Kyligence Zen新特性 五 Kyligence Zen注册篇 六 Kyligence Zen体验篇 七 Kyligence Zen实战篇 7
  • 安卓百度地图开发(三)在定位图层获取指南针角度

    在官方文档中没有给出获取角度的方法 这里使用了安卓自带的传感器获取指南针角度 首先定义传感器和角度 private SensorManager mSensorManager double degree 0 在oncreate方法中初始化传感
  • Xmind使用技巧

    新建图表 根据需求 可新建为空白图表或模板图表 空白图 模板 提高工作效率 其中因果分析 鱼普 图 SWOT分析 比较与对比 读书笔记等常用 也可以新建空白图 改变鱼头的左右方向 相比就是样式没有模板的那么丰富
  • 操作系统 请求分页存储管理

    目录 请求分页存储管理中的页表机制 缺页中断机构 地址转换 页置换算法 页分配和页置换策略 工作集及抖动现象的消除 请求分页存储管理的优缺点 请求分页存储管理中的页表机制 系统需要解决的问题 系统如何获知进程当前所需页面不在主存 当发现缺页
  • 记录一次FISCO BCOS的console启动失败的问题(create BcosSDK failed for the number of available peers is 0)

    记录一次FISCO BCOS的console启动失败的问题 报错 create BcosSDK failed for the number of available peers is 0 版本 FISCO BCOS v2 7 0 conso
  • 新多模态大模型霸榜!支持图文混合输入,不懂知识还能现学

    克雷西 发自 凹非寺量子位 公众号 QbitAI 多模态大模型家族 又有新成员了 不仅能将多张图像与文本结合分析 还能处理视频中的时空关系 这款免费开源的模型 在MMbench和MME榜单同时登顶 目前浮动排名也保持在前三位 MMBench
  • 物联网的应用场景

    随着物联网技术的不断发展和普及 它已经在各个领域展现出了巨大的潜力和前景 下面将会探讨物联网的应用前景 1 智能家居 智能家居是物联网技术最广泛应用的领域之一 通过智能家居设备 人们可以在任何时间 任何地点通过手机 平板电脑等设备远程控制家
  • 深度学习原理分析之数据不足与过拟合

    人们常常知道若干种解决过拟合的方法但不知其因 本文对其进行原理剖析 一个模型所能提供的信息一般来源于两个方面 一是训练数据中蕴含的信息 二是在模型的形成过程中 包括构造 学习 推理等 人们提供的先验信息 当训练数据不足时 说明模型从原始数据
  • Spring Boot官方例子《Developing Your First Spring Boot Application》无法运行

    官方的第一个例子就卡住了 https docs spring io spring boot docs current reference htmlsingle getting started first application 按照要求 一