ContentNegotiatingViewResolver is a special Spring MVC view resolver that doesn't resolve views itself but instead delegates to other configured view resolvers based on the format the client is requesting. It picks the best matching view — JSON, XML, or HTML — by inspecting the request's Accept header, URL extension, or a format parameter.
Key Points: • It allows the same controller and view name to be rendered in multiple formats without duplicating handler logic. • Content negotiation strategy can be driven by the Accept header, a path extension like .json, or a query parameter such as ?format=xml. • It delegates to a chain of underlying resolvers (e.g. a JSON view resolver and a JSP view resolver) and picks the first one matching the negotiated media type. • It reduces the need for separate REST and web-page controllers when both formats are genuinely needed for the same resource. • Modern Spring Boot apps largely favor @RestController with Jackson-based serialization for pure APIs, so ContentNegotiatingViewResolver is more common in mixed, older-style MVC applications.
Example: A single /report/{id} endpoint could return an HTML report page for browsers by default, but an automated client sending Accept: application/json for the same URL would get a JSON representation instead.
Interview Tip: A concise interview answer is:
"ContentNegotiatingViewResolver picks the right view format — JSON, XML, or HTML — based on what the client requests, usually via the Accept header. It's useful when the same endpoint needs to serve multiple response formats without duplicating controller logic, though in pure REST APIs, @RestController with Jackson usually replaces this need."