Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Spring Framework FAQ: Top Questions

28. What is Content Negotiation in Spring MVC?

Content Negotiation is the mechanism used by Spring MVC to determine the format of the response (e.g., JSON, XML, HTML) based on request headers or path extensions.

📘 Behavior:

  • Checks Accept header, URL suffix, or query param.
  • Maps content type to appropriate message converter.

📥 Example:

@GetMapping(value = "/data", produces = "application/json")
public Data getData() {
  return new Data("example");
}

🏆 Expected Output:

Returns JSON if requested via Accept: application/json

🛠️ Use Cases:

  • Supporting multiple response formats with a single controller method.
  • Flexible API endpoint responses based on client needs.