Spring Boot部署远程服务器出现JSP页面404未找到问题求助
Hey, I’ve run into this exact headache before—Spring Boot’s default jar packaging doesn’t play nicely with JSPs, which is why everything works locally in IDEA but breaks when you deploy the jar to your DigitalOcean server. Let’s break down the fixes step by step:
Why this happens
Spring Boot’s embedded Tomcat handles JSPs differently when running from a jar vs. in your IDE. When you build a jar, the JSP files aren’t packaged into a directory the embedded server can access at runtime—they get tucked away in a place the view resolver can’t find.
Fix 1: Switch to WAR packaging (most reliable)
This is the standard, foolproof solution for JSP-based Spring Boot apps:
- Update your
pom.xmlto set packaging to war:<packaging>war</packaging> - Mark the embedded Tomcat dependency as
providedso it doesn’t conflict with the full Tomcat instance on your server:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> - Make your main application class extend
SpringBootServletInitializerto enable WAR deployment compatibility:@SpringBootApplication public class YourAppApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(YourAppApplication.class); } public static void main(String[] args) { SpringApplication.run(YourAppApplication.class, args); } } - Build the war with
mvn clean package, then upload it to your Ubuntu server’s Tomcatwebappsdirectory. Start Tomcat, and your JSPs will be accessible athttp://your-server-ip:8080/your-app-name/home.
Fix 2: Adjust JSP location for jar packaging (less reliable)
If you really want to stick with a jar deployment, you need to move your JSPs to a location the embedded Tomcat can find inside the jar:
- Move all your JSP files from
src/main/webapp/WEB-INF/jsp/tosrc/main/resources/META-INF/resources/WEB-INF/jsp/ - Keep your
application.propertiesconfiguration unchanged:spring.mvc.view.prefix: /WEB-INF/jsp/ spring.mvc.view.suffix: .jsp - Build and run the jar as usual with
java -jar your-app.jar. Note that this method has compatibility quirks with some Spring Boot versions, so WAR is still the safer bet.
Quick verification checks
- Use
jar tf your-app.jaron your server to confirm your JSP files exist in the expected directory. If they’re missing, your build configuration is the issue. - Check server logs (run
tail -f logs/spring.logor similar) for view resolution errors—this will confirm if the server is even trying to locate the JSPs. - Double-check your access URL: for jar deployments, it’s
http://your-server-ip:8080/home; for WAR deployments, you need to include the app name (e.g.,http://your-server-ip:8080/your-app-war-name/home).
内容的提问来源于stack exchange,提问作者Mukhamedali Zhadigerov

