Bird
0
0

How can you return a 302 Found redirect status with a Location header pointing to "/home" in a Spring Boot controller using ResponseEntity?

hard📝 Application Q9 of 15
Spring Boot - Request and Response Handling
How can you return a 302 Found redirect status with a Location header pointing to "/home" in a Spring Boot controller using ResponseEntity?
Areturn ResponseEntity.status(HttpStatus.FOUND).header("Location", "/home").build();
Breturn new ResponseEntity<>("/home", HttpStatus.FOUND);
Creturn "redirect:/home";
Dreturn ResponseEntity.created(URI.create("/home")).build();
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to set redirect status and header

    ResponseEntity.status(HttpStatus.FOUND) sets 302 status, and header("Location", "/home") sets the redirect URL.
  2. Step 2: Analyze other options

    return new ResponseEntity<>("/home", HttpStatus.FOUND); sets body incorrectly, return "redirect:/home"; is Spring MVC redirect syntax but not ResponseEntity, return ResponseEntity.created(URI.create("/home")).build(); sets 201 Created with Location, not 302 Found.
  3. Final Answer:

    return ResponseEntity.status(HttpStatus.FOUND).header("Location", "/home").build(); -> Option A
  4. Quick Check:

    Use status + header for redirect with ResponseEntity [OK]
Quick Trick: Set 302 and Location header with ResponseEntity.status().header() [OK]
Common Mistakes:
  • Using created() for 302 redirect
  • Returning body instead of header for redirect
  • Confusing redirect string with ResponseEntity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes