-
问题
不占用端口启动时,程序正常执行,下面为两种springboot启动方式:
//使用纯后台方式启动,不占用端口,springboot2.0以后的写法
new SpringApplicationBuilder().sources(Application.class).web(WebApplicationType.NONE).run(args);
//使用web方式启动,会占用端口
SpringApplication.run(Application.class, args);
后来需要开放端口,使用HTTP调用,就修改了Springboot启动方式,然后就无法启动了,并抛出以下异常:
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:333)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265)
at com.linkcld.analysis.Application.main(Application.java:24)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152)
... 8 common frames omitted
解决方法:
看有些人回答比较复杂,什么内嵌tomcat冲突的,什么缺少注解的,我的情况就比较简单了,添加 spring-boot-starter-web 依赖即可:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--版本根据需要设置-->
<version>2.0.5.RELEASE</version>
</dependency>
问题解决。