Programming & Development / March 27, 2025

Handling and Returning 404 Not Found in Spring Boot Applications

spring boot 404 not found return 404 spring controller spring responseentity 404 spring responsestatusexception custom 404 error spring boot spring restcontroller 404 global exception handler spring boot spring boot error handling

In Spring Boot applications, a 404 Not Found error typically means the requested URL doesnโ€™t match any controller endpoint, or you're deliberately signaling that a resource wasn't found. This article covers:

  • Common causes of 404s in Spring Boot
  • How to debug unexpected 404 errors
  • Multiple ways to explicitly return 404 responses in REST APIs
  • How to use custom exceptions and global exception handling

Use these strategies to build reliable, user-friendly RESTful services.

โ— Common Reasons Spring Boot Returns a 404

๐Ÿ”น 1. Incorrect URL or Path

Ensure that the URL you're accessing matches your controller's path and method.

java

@RestController
@RequestMapping("/api")
public class MyController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

// URL must be: http://localhost:8080/api/hello

๐Ÿ”น 2. Controller Not Scanned

Controllers must be in packages scanned by Spring Boot.

java

@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

// OR explicitly define scanning:
@SpringBootApplication
@ComponentScan(basePackages = "com.example.controllers")

๐Ÿ”น 3. Wrong HTTP Method

Check that your controller method matches the request method (e.g., GET vs POST).

java

// If using POST, make sure your client sends POST
@PostMapping("/submit")
public String submitData() {
    return "Submitted!";
}

๐Ÿ”น 4. Missing View in Thymeleaf Apps

If using Thymeleaf, ensure templates exist in src/main/resources/templates.

๐Ÿ”น 5. Custom Error Handling Conflicts

Check for misconfigured @ControllerAdvice that may intercept or redirect requests.

โœ… How to Explicitly Return a 404 Response

โœ… 1. Using ResponseStatusException

A quick and direct way to throw a 404:

java

@GetMapping("/item/{id}")
public String getItem(@PathVariable String id) {
    boolean exists = false;
    if (!exists) {
        throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Item not found");
    }
    return "Item details";
}

โœ… 2. Returning ResponseEntity

java

@GetMapping("/item/{id}")
public ResponseEntity<String> getItem(@PathVariable String id) {
    boolean exists = false;
    if (!exists) {
        return new ResponseEntity<>("Item not found", HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<>("Item details", HttpStatus.OK);
}

โœ… 3. Custom Exception with @ResponseStatus

java

@ResponseStatus(HttpStatus.NOT_FOUND)
public class ItemNotFoundException extends RuntimeException {
    public ItemNotFoundException(String message) {
        super(message);
    }
}

// Controller
@GetMapping("/item/{id}")
public String getItem(@PathVariable String id) {
    throw new ItemNotFoundException("Item not found");
}

โœ… 4. Global Exception Handling with @ControllerAdvice

Create a centralized handler for cleaner code:

java

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ItemNotFoundException.class)
    public ResponseEntity<String> handleItemNotFound(ItemNotFoundException ex) {
        return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
    }
}

๐Ÿงช Bonus Tips for Troubleshooting

  • โœ… Enable Debug Logging to view endpoint mappings:
yaml

logging:
  level:
    org.springframework.web: DEBUG
  • โœ… Check Request URL and Method in Postman or browser
  • โœ… Test with curl to confirm endpoint response

๐Ÿ“Œ Summary Table

StrategyPurposeResponseStatusExceptionQuick way to return 404ResponseEntity with 404 statusFine-grained response controlCustom Exception with @ResponseStatusClean semantic exception mapping@ControllerAdvice global handlerCentralized error handling


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex