SpringBoot項目部署到tomcat

SpringBoot部署到tomcat

1、修改maven.xml

一、添加<.packaging>war</.packaging>,打包爲war包html

<packaging>war</packaging>

SpringBoot項目部署到tomcat

二、不使用SpringBoot內置的Tomcat,添加web

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

SpringBoot項目部署到tomcat

2、修改application.properties文件,添加 content-path

server.servlet.context-path=/MyProject

SpringBoot項目部署到tomcat

3、修改啓動文件main方法,讓該方法繼承自SpringBootServletInitializer,而且重寫configure方法:

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(FileuploadApplication.class);
}

修改後的啓動文件爲:spring

package qiu.fileupload;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class FileuploadApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(FileuploadApplication.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(FileuploadApplication.class);
    }
}

SpringBoot項目部署到tomcatSpringBoot項目部署到tomcat

4、打包

一、在項目的根目錄下面執行命令:

mvn clean package
打包成功後,在項目的根目錄下面會多出一個target目錄,該目錄下面有一個war包,名爲:fileupload-0.0.1-SNAPSHOT.war。tomcat

SpringBoot項目部署到tomcat

二、將fileupload-0.0.1-SNAPSHOT.war更名爲yml文件中context-path的名字MyProject

SpringBoot項目部署到tomcat

5、拷貝到tomcat的webapps目錄,重啓tomcat。

6、訪問測試

注:此時訪問的端口以tomcat的端口爲準。
http://localhost:8080/MyProject/register.htmlapp

SpringBoot項目部署到tomcat

相關文章
相關標籤/搜索