How to return 404 response status in Spring Boot @ResponseBody?

How to return 404 response status in Spring Boot @ResponseBody?

In Spring Boot, you can return a 404 response status using the @ResponseBody annotation along with a ResponseEntity object or by throwing an exception. Here are two common approaches to achieve this:

  • Using ResponseEntity:

You can return a 404 response status with a custom message using ResponseEntity like this:

import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class MyController { @GetMapping("/resource/{id}") public ResponseEntity<String> getResource(@PathVariable Long id) { // Check if the resource with the given ID exists if (resourceNotFound(id)) { return ResponseEntity.status(HttpStatus.NOT_FOUND) .body("Resource not found"); } // If resource exists, return it return ResponseEntity.ok("Resource found"); } // Simulate resource not found private boolean resourceNotFound(Long id) { // Implement your logic to check if the resource exists // Return true if not found, false otherwise return true; } } 

In this example, if the resourceNotFound method returns true, a 404 response status with the message "Resource not found" will be returned. Otherwise, a 200 OK response status with the message "Resource found" will be returned.

  • Throwing an Exception:

Another way to return a 404 response status is by throwing an exception and handling it with an exception handler:

import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; @ControllerAdvice public class ExceptionController { @ExceptionHandler(ResourceNotFoundException.class) @ResponseStatus(HttpStatus.NOT_FOUND) @ResponseBody public String handleResourceNotFoundException(ResourceNotFoundException ex) { return "Resource not found"; } } @RestController @RequestMapping("/api") public class MyController { @GetMapping("/resource/{id}") public String getResource(@PathVariable Long id) { // Check if the resource with the given ID exists if (resourceNotFound(id)) { throw new ResourceNotFoundException(); } // If resource exists, return it return "Resource found"; } // Simulate resource not found private boolean resourceNotFound(Long id) { // Implement your logic to check if the resource exists // Return true if not found, false otherwise return true; } } // Custom exception class @ResponseStatus(HttpStatus.NOT_FOUND) class ResourceNotFoundException extends RuntimeException { } 

In this approach, when the resourceNotFound method returns true, a custom ResourceNotFoundException is thrown, and Spring Boot's exception handling mechanism (@ControllerAdvice and @ExceptionHandler) takes care of returning a 404 response status with the message "Resource not found."

You can choose the approach that best fits your application's requirements and coding style.


More Tags

publisher swiperefreshlayout nosql x11-forwarding equation-solving right-to-left fixtures xlrd dsl drawerlayout

More Java Questions

More Mixtures and solutions Calculators

More Statistics Calculators

More Fitness Calculators

More Mortgage and Real Estate Calculators