亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot項(xiàng)目打包成war包并部署在tomcat上運(yùn)行的操作步驟

 更新時(shí)間:2024年03月29日 09:46:46   作者:濤哥是個(gè)大帥比  
我們開發(fā) SpringBoot 項(xiàng)目有時(shí)我們會(huì)需要打包成 war 包,放入外置的 Tomcat 中進(jìn)行運(yùn)行,或者使用工具idea直接啟動(dòng),便于開發(fā)調(diào)試,本文給大家分享SpringBoot項(xiàng)目打包成war包并部署在tomcat上運(yùn)行的操作步驟,感興趣的朋友一起看看吧

項(xiàng)目場(chǎng)景:

        正常情況下,我們開發(fā) SpringBoot 項(xiàng)目,由于內(nèi)置了Tomcat,所以項(xiàng)目可以直接啟動(dòng),部署到服務(wù)器的時(shí)候,直接打成 jar 包,就可以運(yùn)行了。

        有時(shí)我們會(huì)需要打包成 war 包,放入外置的 Tomcat 中進(jìn)行運(yùn)行,或者使用工具idea直接啟動(dòng),便于開發(fā)調(diào)試。

實(shí)現(xiàn)步驟

1、將pom文件打包方式更改為 war

<packaging>war</packaging>

2、 排除內(nèi)置 Tomcat

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<!-- 排除內(nèi)置的tomcat -->
	<exclusions>
		<exclusion>
			<artifactId>org.springframework.boot</artifactId>
			<groupId>spring-boot-starter-tomcat</groupId>
		</exclusion>
	</exclusions>
</dependency>

3、添加tomcat依賴,需要用到 servlet-api 的相關(guān) jar 包 

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
	<!-- tomcat范圍改成provided,否則后面就會(huì)出問(wèn)題,tomcat無(wú)法解析jsp -->
	<scope>provided</scope>
</dependency>

4、  繼承 SpringBootServletInitializer 并重寫 configure 方法

新建文件文件名隨意,或者直接修改啟動(dòng)類繼承 SpringBootServletInitializer 并重寫 configure 方法,也是一樣的。

package com.test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
/**
 * 注意,使用war方式部署,需要開啟此類
 *
 */
public class ServletInitializer extends SpringBootServletInitializer {
    @Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
        return application.sources(ApplicationMain.class);  
    }
} 

部署方式:

方式一:使用外部tomcat啟動(dòng)

1、利用maven命令打包

2、 將打的war包,復(fù)制粘貼到tomcat的webapps目錄下(不用解壓,啟動(dòng)tomcat自動(dòng)解壓)

3、啟動(dòng)tomcat 

在tomcat安裝目錄下的bin目錄下面找到startup.bat命令,啟動(dòng)tomcat

 4、啟動(dòng)結(jié)果

war包自動(dòng)解壓了

5、 測(cè)試結(jié)果,訪問(wèn)swagger頁(yè)面:

訪問(wèn)路徑這里需要注意,原來(lái)我們?cè)赼pplication.properties配置的訪問(wèn)路徑已經(jīng)不生效了。

這是原來(lái)訪問(wèn)路徑:http://localhost:8080/testservice/swagger-ui.html

#已經(jīng)不生效了
server.servlet.context-path=/testservice

現(xiàn)在的訪問(wèn)路徑:

http://localhost:[端口號(hào)]/[打包項(xiàng)目名]/

比如我現(xiàn)在的訪問(wèn)路徑就是:

http://localhost:8080/spring-boot-test-war/swagger-ui.html

 spring-boot-test-war是我打包后的項(xiàng)目名,這個(gè)可以改的。

 方式二:使用工具idea直接啟動(dòng)

1、配置web.xml文件

點(diǎn)擊File->Project Structure

 創(chuàng)建src/main/webapp和web.xml

 此時(shí)項(xiàng)目結(jié)構(gòu)圖如下:

 2、配置artifacts

配置完后,tomcat啟動(dòng)才能找到這個(gè)war包,會(huì)生成out目錄輸出文件。

當(dāng)然你也可以選擇target下面已經(jīng)打包好的war包,但是這樣有個(gè)缺點(diǎn),就是每次改文件你都需要用maven重新打包,輸出到target目錄下,不方便開發(fā)。

3、配置tomcat

在IDEA右上角的項(xiàng)目運(yùn)行列表中選中 Edit Configurations

進(jìn)入新的窗口點(diǎn)擊"+",找到Toncat Server中的Local進(jìn)行點(diǎn)擊,配置Tomcat路徑

4、tomcat 選擇啟動(dòng)的war包

這里注意選擇exploded結(jié)尾的,才是out目錄輸出的

Application context上下文配置訪問(wèn)路徑 

訪問(wèn)路徑這里需要注意,原來(lái)我們?cè)赼pplication.properties配置的訪問(wèn)路徑已經(jīng)不生效了。

#已經(jīng)不生效了
server.servlet.context-path=/testservice

現(xiàn)在的訪問(wèn)路徑:

http://localhost:8080/testservice/swagger-ui.html

 testservice是我Application context上下文配置的訪問(wèn)路徑 ,這個(gè)可以改的。

5、配置tomcat啟動(dòng)默認(rèn)打開的頁(yè)面

6、啟動(dòng)結(jié)果

點(diǎn)擊啟動(dòng)

到此這篇關(guān)于SpringBoot項(xiàng)目打包成war包并部署在tomcat上運(yùn)行的操作步驟的文章就介紹到這了,更多相關(guān)SpringBoot打包成war包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論