Java异常处理004:Springboot项目在tomcat下正常启动,但是无法访问(页面和接口)

Java异常处理004:Springboot项目在tomcat下正常启动,但是无法访问(页面和接口)

解决方案:

第一步,在POM.XML文件引入依赖

        <!-- 打war包时加入此项, 告诉spring-boot tomcat相关jar包用外部的,不要打进去 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
       <!-- springboot自带的tomcat并没有携带tomcat-embed-jasper的依赖,如果不引入tomcat-embed-jasper依赖,使用SPringboot启动项目则会无法成功-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

第二部,修改Springboot启动类,继承SpringBootServletInitializer类(作用是为了支持可以不使用web.xml)

public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
改成
public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

对SpringBootServletInitializer类的理解,请参考:https://blog.csdn.net/zq17865815296/article/details/79464403

原文地址:https://www.cnblogs.com/wobuchifanqie/p/11680755.html