Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What is Content Negotiation in Spring Boot REST APIs? स्प्रिंग बूट REST API में कंटेंट नेगोशिएशन क्या है?

Answer

Content negotiation is the process by which Spring determines the representation format (JSON, XML, etc.) to return based on the client's Accept header, a URL path extension, or a query parameter. Spring Boot uses JSON by default when spring-boot-starter-web is present, since Jackson is on the classpath.

To support additional formats like XML, you add the corresponding converter dependency (e.g. Jackson's XML module) and Spring automatically negotiates based on the request's Accept header without any controller code changes, since the HttpMessageConverter mechanism handles the format selection.

// Client requests XML:
// GET /api/users/1
// Accept: application/xml
//
// Same controller method returns XML automatically
// once jackson-dataformat-xml is on the classpath.

कंटेंट नेगोशिएशन वह प्रक्रिया है जिससे स्प्रिंग यह तय करता है कि क्लाइंट के Accept हेडर, URL पाथ एक्सटेंशन, या क्वेरी पैरामीटर के आधार पर कौन-सा प्रतिनिधित्व फॉर्मेट (JSON, XML आदि) लौटाया जाए। जब spring-boot-starter-web मौजूद हो, तो स्प्रिंग बूट डिफ़ॉल्ट रूप से JSON उपयोग करता है क्योंकि Jackson क्लासपाथ पर होता है।

XML जैसे अतिरिक्त फॉर्मेट सपोर्ट करने के लिए संबंधित कन्वर्टर डिपेंडेंसी जोड़ी जाती है, और स्प्रिंग बिना किसी कंट्रोलर कोड बदलाव के Accept हेडर के आधार पर स्वचालित रूप से नेगोशिएट करता है।

// क्लाइंट XML रिक्वेस्ट करता है:
// GET /api/users/1
// Accept: application/xml

Was this answer clear?