Spring Boot 常规错误一览及解决方案

来源:互联网 发布:mac写入ntfs硬盘 编辑:程序博客网 时间:2024/05/20 12:23

想法很单纯,将自己在Spring Boot学习过程中遇到的各种麻烦列出来,并提供解决方案待查。

正题开始,遇到错误可通过报错信息对号入座:


错误提示:java.net.BindException: Address already in use: bind

推测原因:一开始接触Spring Boot时很常见的错误,端口已绑定。之前已启动Application,Spring Boot会启动内嵌的Tomcat,并绑定端口8080启动前端服务。作为Web应用,程序自己不会终结。而用户也没有手动结束程序,该端口就始终被绑定,再次启动必然会报此问题。

解决方案:打开Windows进程管理器结束javaw.exe,重新运行。并在每次启动程序前,结束之前的运行。


错误提示:Spring Boot Error: java.lang.NoSuchMethodError

推测原因:很显然,就是找不到指定的方法。

解决方案:仔细搜索报错信息中的方法名,查看出错类中是否缺少某方法。笔者此次报错由于org.springframework.core.ResolvableType.forInstance方法找不到所致,又想起之前在pom.xml中移除了parent依赖,想起是否改文件没有完整下载。查询了官网说明:当移除parent依赖时,需要增加spring-boot-dependencies的依赖。因此pom.xml中在<dependencies>前新增以下依赖,问题解决^_^

[html] view plain copy
  1. <dependencyManagement>  
  2.   <dependencies>  
  3.     <dependency>  
  4.       <groupId>org.springframework.boot</groupId>  
  5.       <artifactId>spring-boot-dependencies</artifactId>  
  6.       <version>1.3.3.RELEASE</version>  
  7.       <type>pom</type>  
  8.       <scope>import</scope>  
  9.     </dependency>  
  10.   </dependencies>  
  11. </dependencyManagement>  

错误提示:java -jar myApplication.jar 

系统报错:Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

推测原因:根据系统提示,依次寻找报错源头,最终定位在EmbeddedServletContainerFactory.class这个文件,原来它缺少了@Bean注解。至少表面上看是这样,具体原因待高手解释。

解决方案:在Application.java主程序入口中加入以下代码:

[java] view plain copy
  1. @Bean  
  2. public EmbeddedServletContainerFactory servletContainer() {  
  3.          
  4.     TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();  
  5.     return factory;  
  6.          
  7. }  
原创粉丝点击