Bird
0
0

Why does this Spring Boot method fail content negotiation?

medium📝 Debug Q7 of 15
Spring Boot - Request and Response Handling
Why does this Spring Boot method fail content negotiation?
@GetMapping(value = "/user", produces = "application/json")
public User getUser(@RequestHeader("Accept") String accept) {
  if (!accept.equals("application/json")) {
    throw new RuntimeException("Unsupported format");
  }
  return new User("Alice");
}
AMethod lacks @ResponseBody annotation
BProduces attribute should be 'consumes'
CManually checking Accept header breaks Spring Boot's automatic negotiation
DAccept header cannot be read in controller methods
Step-by-Step Solution
Solution:
  1. Step 1: Understand Spring Boot automatic content negotiation

    Spring Boot automatically handles Accept header and selects response format.
  2. Step 2: Identify manual Accept header check issue

    Manually checking Accept header and throwing exceptions bypasses built-in negotiation and causes errors.
  3. Final Answer:

    Manually checking Accept header breaks Spring Boot's automatic negotiation -> Option C
  4. Quick Check:

    Let Spring Boot handle Accept header automatically [OK]
Quick Trick: Avoid manual Accept header checks; trust Spring Boot [OK]
Common Mistakes:
  • Manually parsing Accept header in controller
  • Missing @ResponseBody on REST methods
  • Confusing produces and consumes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes