Spring Boot 404 Errors in Production
Spring Boot apps returning 404 in production but working locally usually have context path, profile, or packaging issues.
Context Path Changed
A context path in production changes all your URLs:
# application-production.yml
server:
servlet:
context-path: /api
# Now /users becomes /api/usersFix: Check which profile is active and what context path it sets.
Component Scan Missing Your Controllers
// If your Application class is in com.myapp
// but controllers are in com.myapp.api.controllers
// Make sure the base package is correct:
@SpringBootApplication(scanBasePackages = "com.myapp")
public class Application { }Static Resources Not Found
# Default locations: classpath:/static/, classpath:/public/
# If you changed it:
spring:
web:
resources:
static-locations: classpath:/static/,classpath:/public/In a JAR, static files must be in src/main/resources/static/.
WAR vs JAR Deployment
WAR files deployed to an external Tomcat use the WAR filename as context path:
# myapp.war deployed to Tomcat
# Routes become: /myapp/api/users
# Rename to ROOT.war for root context pathWhitelabel Error Page
Spring Boot's default 404 shows the Whitelabel error page. Customize it:
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<Map<String, String>> handleNotFound() {
return ResponseEntity.status(404)
.body(Map.of("error", "Route not found"));
}
}Enable it:
spring:
mvc:
throw-exception-if-no-handler-found: trueBugsly tracks 404 rates in Spring Boot and alerts when they spike after deployment, helping you catch route configuration issues early.
Try Bugsly Free
AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.
Get Started FreeRelated Articles
How to Fix Validationerror in Flask When Deploying
Fix Validationerror in your Flask app when deploying. Understand the root cause and apply the right solution.
Read moreFix Session Error in FastAPI
Step-by-step guide to fix Session Error in FastAPI. Includes root cause analysis, code examples, debugging tips, and prevention strategies.
Read moreFix ResizeObserver Loop Limit Exceeded in Vue
Step-by-step guide to fix ResizeObserver Loop Limit Exceeded in Vue. Includes root cause analysis, code examples, debugging tips, and prevention strateg...
Read moreHow to Fix Validationerror in Python When Deploying
Struggling with Validationerror in Python when deploying? This guide explains why it happens and how to resolve it quickly.
Read more