1. 程式人生 > >springboot打war包後部署到tomcat後訪問返回404錯誤

springboot打war包後部署到tomcat後訪問返回404錯誤

build pin 情況下 ont name 個人 ble ext sim

springboot打war包後部署到tomcat後訪問返回404錯誤

1、正常情況下,修改打包方式為war

<packaging>war</packaging>


2、啟動類繼承SpringBootServletInitializer,重寫configure方法

@SpringBootApplication
public class SmallProgramApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

return builder.sources(SmallProgramApplication.class);
}

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

我因為添加了servlet,在啟動類實現了ServletContextInitializer,並重寫了onStartup方法,把servlet加進來

@SpringBootApplication
public class SmallProgramApplication extends SpringBootServletInitializer implements ServletContextInitializer
{

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

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addServlet(WeiXinServlet.class.getSimpleName(),new WeiXinServlet())//
.addMapping("/wx");
}

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



結果是:在idea啟動訪問正常,打war包後,tomcat啟動正常,但看不到springboot啟動日誌,能訪問servlet,但不能訪問controller
我個人判斷出現這種原因的可能是:
重寫onStartup方法,將SpringBootServletInitializer中的springboot的onStartup方法覆蓋,所以導致springboot沒有成功啟動。

因為這個坑了很久,所以記錄下來。

springboot打war包後部署到tomcat後訪問返回404錯誤