SpringBoot打war包,并且部署到Tomcat服务器,运行报错404(springboot专属404页面)【完美解决】

SpringBoot打war包,并且部署到Tomcat服务器,运行报错404(springboot专属404页面)【完美解决】


参考文档:idea下springboot打包成jar包和war包,并且可以在外部tomcat下运行访问到

修改pom文件

1、packaging由jar变成war

    <groupId>com.wangysgroupId>
    <artifactId>wechatProjectartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>

2、剔除springboot本身Tomcat配置

spring boot 本身是内置了tomcat的,但是我们在外面的tomcat部署项目的时候,就必须把springboot本身的tomcat剔除掉,否则会形成冲突!

provided 作用

provided表明该包只在编译和测试的时候用!


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-tomcatartifactId>
    
    <scope>providedscope>
dependency>

<dependency>
    <groupId>org.apache.tomcat.embedgroupId>
    <artifactId>tomcat-embed-jasperartifactId>
    
    
dependency>

3、将pom文件中的servlet-api.jar包设置为只有在编译和测试的时候用!因为tomcat本身也有servlet-api.jar包,结果会产生冲突!

 <dependency>
     <groupId>javax.servletgroupId>
     <artifactId>javax.servlet-apiartifactId>
     <version>3.1.0version>
     
     <scope>providedscope>
dependency>
<dependency>
     <groupId>javax.servlet.jspgroupId>
     <artifactId>jsp-apiartifactId>
     <version>2.2version>
     <scope>providedscope>
dependency>

4、如果有下面这个插件spring-boot-maven-plugin,请将其注释,否则会打包错误!

<build>
     
     <finalName>weChatProjectfinalName>
    <plugins>
        
        <plugin>
             <groupId>org.springframework.bootgroupId>
             <artifactId>spring-boot-maven-pluginartifactId>
             <version>2.0.1.RELEASEversion>
             <configuration>
             <mainClass>${start-class}mainClass>
             <layout>ZIPlayout>
             configuration>
             <executions>
                <execution>
                   <goals>
                      <goal>repackagegoal>
                   goals>
                execution>
             executions>
        plugin>
    plugins>
build>

2、继承SpringBootServletInitializer方法

SpringBoot启动类继承SpringBootServletInitializer方法,并重写configure方法

点解:
springboot项目打成war包部署到tomcat时需要改变启动方式,否则运行tomcat时war包只会解压,但是加载不了

@RestController
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class WeChatApplication extends SpringBootServletInitializer{

    @RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }
    /**
     * 需要把web项目打成war包部署到外部tomcat运行时需要改变启动方式
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WeChatApplication.class);
    }

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

3、默认配置下的访问路径(至关重要)

默认配置下,将war包发布到tomcat服务器,需要加war包的名称!!!!
http://localhost:8080/war包名/@RequestMapping.value访问了

你可能感兴趣的:(SpringBoot)