1、正常情况下,修改打包方式为war
<packaging>war</packaging> |
2、启动类继承SpringBootServletInitializer,重写configure方法
@SpringBootApplicationpublic 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加进来
@SpringBootApplicationpublic 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没有成功启动。
因为这个坑了很久,所以记录下来。
原文地址:https://www.cnblogs.com/skyxia/p/10023745.html
时间: 2024-10-13 01:18:39