All posts

Fix NotFoundError in Spring Boot in Production

Resolve 404 errors in production Spring Boot apps caused by context path changes, missing controllers, and static resource handling.

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/users

Fix: 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 path

Whitelabel 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: true

Bugsly 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 Free