Given a Spring Boot controller method annotated with @GetMapping(produces = {"application/json", "application/xml"}), what content type will the server respond with if the client sends an Accept: application/json header?
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping(value = "/data", produces = {"application/json", "application/xml"}) public Data getData() { return new Data("example", 123); } } record Data(String name, int value) {}
Check the Accept header and the produces attribute in the controller.
The server uses content negotiation to match the client's Accept header with the produces types. Since the client requests application/json, the server responds with JSON.
In Spring Boot, which of the following @GetMapping annotations correctly specifies that the method can produce both JSON and XML responses?
Look at the correct Java array syntax for the produces attribute.
The produces attribute expects a String array. Option B uses the correct Java array syntax with curly braces and double quotes.
A client sends a request with header Accept: application/pdf to a Spring Boot endpoint that produces application/json only. What causes the 406 error?
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class PdfController { @GetMapping(value = "/report", produces = "application/json") public Report getReport() { return new Report("Monthly", 100); } } record Report(String title, int count) {}
Think about what 406 status means in HTTP.
HTTP 406 means the server cannot generate a response matching the client's Accept header. Since the server only produces JSON and client wants PDF, it returns 406.
Consider a Spring Boot controller method annotated with @GetMapping(produces = {"application/json", "application/xml"}). If the client sends a request without an Accept header, what content type will the server respond with?
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DefaultController { @GetMapping(value = "/default", produces = {"application/json", "application/xml"}) public Info getInfo() { return new Info("default", true); } } record Info(String name, boolean active) {}
Think about the default content type Spring Boot uses when no Accept header is specified.
When no Accept header is sent, Spring Boot defaults to the first type listed in the produces array, which is usually JSON.
Spring Boot has multiple HttpMessageConverters registered, including JSON and XML converters. When a controller method produces both application/json and application/xml, how does Spring Boot decide which converter to use for the response?
Consider how HTTP content negotiation works with Accept headers and converters.
Spring Boot uses the Accept header to find the best matching media type among registered converters and uses that converter to serialize the response.