application.yaml中任何配置。
很简单的spring boot demo而已。在本地用直接run启动。完全ok。
并且浏览器访问localhost:8080(这里application.yaml并未配置server.servlet. context-path),可以在浏览器看到
2、问题就是这个简单的demo在本地用idea中,run启动,完全没有任何问题,但是使用maven打包成war包,然后使用tomcat启动,再去访问localhost:8080/项目名 (ps:这里跟springboot中用内置tomcat运行,访问路径不太一样)。
使用maven打完包后:demo-0.0.1-SNAPSHOT.war,将其改名demo.war丢到tomcat的webapps下。在bin目录中运行.status.bat运行tomcat。
访问浏览器localhost:8080/demo:
3.解决 3.1 引入spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-tomcat provided 3.2 启动类 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;@SpringBootApplicationpublic class DebugDemoApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(DebugDemoApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DebugDemoApplication.class); }}以上修改完毕后,使用idea再次run启动,访问浏览器localhost:8080:
再次打包、重命名、并启动tomcat服务器,查看放在外部tomcat服务器中的效果:
所以,综上问题已解决。
ps:
本地springboot启动与tomcat服务器启动访问的接口的路径不太一致,这里解决也很简单,在spring boot的主配置文件application.yml中添加一段配置:
server: servlet: context-path: /demo #项目名