Spring Boot 启动失败?10种常见报错及解决方案 Spring Boot 项目启动时报错是每个Java开发者都会遇到的事。这篇文章整理了10种最常见的启动报错附解决方案。1. Failed to configure a DataSource报错信息Failed to configure a DataSource: url attribute is not specified原因项目中引入了数据库依赖但没有配置数据源。解决如果不需要数据库在启动类上排除自动配置SpringBootApplication(exclude {DataSourceAutoConfiguration.class})如果需要数据库检查 application.yml 中的数据源配置是否正确。2. Port was already in use报错信息Web server failed to start. Port 8080 was already in use.原因端口被占用。解决找到占用端口的进程并关闭或在配置中换一个端口server: port: 80813. 数据库驱动版本不匹配报错信息java.lang.AbstractMethodError: com.mysql.cj.jdbc.Driver.getMajorVersion()原因MySQL驱动版本与数据库版本不匹配。解决MySQL 8.x 使用较新版本的驱动dependency groupIdcom.mysql/groupId artifactIdmysql-connector-j/artifactId version8.0.33/version /dependency4. 包扫描路径不对现象项目能启动但访问接口返回404或者Autowired注入失败。原因启动类的位置不对默认扫描启动类所在包及其子包。解决确保启动类放在最外层包下或者手动指定扫描路径SpringBootApplication(scanBasePackages {com.example.controller, com.example.service})5. Maven依赖冲突报错信息java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable原因项目中有多个版本的同一个jar包冲突。解决在 pom.xml 中用exclusions排除冲突的依赖dependency groupIdxxx/groupId artifactIdxxx/artifactId exclusions exclusion groupIdcom.google.guava/groupId artifactIdguava/artifactId /exclusion /exclusions /dependency6. Lombok相关的报错报错信息java: cannot find symbol symbol: method getXxx()原因IDE没有安装Lombok插件或者没有启用注解处理器。解决IDEA安装Lombok插件File → Settings → Plugins → 搜索Lombok启用注解处理器Settings → Build → Compiler → Annotation Processors → Enable7. 配置文件格式错误现象启动时报YamlPropertySourceLoader相关错误。原因yml文件的缩进或格式不正确。解决yml对缩进敏感检查每层的空格数冒号后面必须有一个空格8. Whitelabel Error Page现象启动成功但访问页面显示Whitelabel Error Page。原因没有配置Controller或者Controller路径不对。解决确认Controller类上加了RestController或Controller确认RequestMapping中的路径正确9. 内存不足报错信息java.lang.OutOfMemoryError: Java heap space解决在IDEA或命令行中加大JVM内存java -Xms512m -Xmx1024m -jar your-app.jar10. 端口被系统保留报错信息BindException: Cannot assign requested address解决Windows上某些端口被系统保留换一个端口试试。总结遇到启动报错不要慌先看错误信息的第一行找到真正的异常原因再对症下药。上面这10种覆盖了日常开发中90%的启动报错场景。