一、修改maven.xml
1、添加<.packaging>war</.packaging>,打包為war包
<packaging>war</packaging>
2、不使用SpringBoot內置的Tomcat,添加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
二、修改application.properties文件,添加 content-path
server.servlet.context-path=/wx-server
三、修改啟動文件main方法,讓該方法繼承自SpringBootServletInitializer,並且重寫configure方法:
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(App.class); }
修改后的為:
package com.st; 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 App extends SpringBootServletInitializer{ public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(App.class); } }
在webapp目錄新建WEB-INF,並新建web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>vxsoft-tts</display-name>
</web-app>
四、打包
1、在項目的根目錄下面執行命令:
mvn clean package
打包成功后,在項目的根目錄下面會多出一個target目錄,該目錄下面有一個war包,名為:wx-server.war。
五、拷貝到tomcat的webapps目錄,重啟tomcat。
六、訪問測試
注意:此時訪問的端口以tomcat的端口為准。
http://localhost:8080/wx-server/indexhtml