以前没有系统学习过Springboot,由于最近在公司项目中需要使用,只能一边踩坑一遍学习。还好,有了问题可以google。
比如,昨天遇到如标题所示的问题。准确的说,我的项目中有多个Controller,每个Controller的注解格式一致,如下:
@Slf4j
@RestController
@CrossOrigin(origins ="*")
@RequestMapping("/example1")
public class ExampleController1 {
//业务逻辑
}
然后在项目的Application中做了如下继承了SpringBootServletInitializer并且重写了configure方法,如下:
@SpringBootApplication
public class ExampleApplication extends SpringBootServletInitializer implements WebMvcConfigurer {
@Override
protected SpringApplicationBuilderconfigure(SpringApplicationBuilder application){
return application.sources(ExampleApplication.class);
}
//其他代码
}
好了。首先在IDEA中启动项目并访问localhost:9089/example1,能够正确访问;然后打war包(名为example.war)部署到Tomcat的webapp中,启动后访问localhost:8080/example1,结果访问不了,报404错误。明确不是代码问题,但花了一个下午和晚上的时间排查还是没有解决。今早突然收到一篇博客(地址:https://blog.csdn.net/qq_36659177/article/details/83502873,特别感谢博主),解决了问题!
原来,打成war包发布到Tomcat中后,访问时需要在URL中加上war包名称,将localhost:8080/example1改成localhost:8080/example/example1就可以访问了。
看来是到系统地学习Springboot的时候了,今天立个flag,每天在此记录一篇学习日记。
网友评论