Spring API Controllers: Consumes and Produces Explained

🤔 Ever wondered what consumes and produces actually do in Spring APIs? Most of us write controllers like this 👇 @PostMapping("/users") public User create(@RequestBody User user) { return user; } But Spring gives you more control. 🧠 consumes ➡️ Tells Spring what request content type is allowed. @PostMapping( value = "/users", consumes = "application/json" ) Meaning: Client must send JSON Otherwise → 415 Unsupported Media Type 🧠 produces ➡️ Tells Spring what response format you return. @PostMapping( value = "/users", produces = "application/json" ) Meaning: Client receives JSON Controlled via Accept header ✅ Using both together @PostMapping( value = "/users", consumes = "application/json", produces = "application/json" ) ✔️ Request must be JSON ✔️ Response will be JSON 🧠 Simple mental model consumes → what API accepts produces → what API returns 💡 This is how Spring performs content negotiation internally. Small detail. Big clarity. 💾 Save this — it’s a common interview question. #SpringBoot #Java #BackendEngineering #RESTAPI #SoftwareEngineering  #Programming #DeveloperLife #InterviewPrep #CleanCode

To view or add a comment, sign in

Explore content categories