springboot2.0jar包启动异常

时间:2023-03-08 18:36:35
springboot2.0jar包启动异常

今天碰到一个异常:

08:44:07.214 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.s
: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

提示明显,没有servletweb容器。

按说springboot是有内置容器的。后来查找看到帖子:https://blog.csdn.net/riyunzhu/article/details/63259718

但是我的jdk1.8,没有问题,估计版本兼容问题。按照上述的解决方案,在启动类添加代码:

    @Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory =
new TomcatEmbeddedServletContainerFactory();
return factory;
}

提示找不到 EmbeddedServletContainerFactory类。查找后知道:EmbeddedServletContainerFactory找不到,是springboot2.X之后改了写法。

详情参见帖子:https://blog.csdn.net/l4642247/article/details/81631770

后改为:

    @Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
return factory;
}

打包成功,可以正常启动项目了。

在这里贴下idea快捷键:https://www.cnblogs.com/zhangpengshou/p/5366413.html

idea打包maven项目:https://www.jb51.net/article/141962.htm

以前都用thymeleaf,记录下spring项目支持jsp

<!-- 使用 jsp 必要依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

另外:打包后,启动jar包显示“中没有主清单属性” 需要在配置中将

springboot2.0jar包启动异常

MANIFEST.MF放到main/resources下。

另外需要maven打包jsp需要添加:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<!-- 指定resources插件处理哪个目录下的资源文件 -->
<directory>src/main/webapp</directory>
<!--注意此次必须要放在此目录下才能被访问到 -->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>