Content type negotiation is a process where Spring Boot decides the format (like JSON or XML) to send in the response based on the client's request headers or URL parameters.
Click to reveal answer
beginner
Which HTTP header does Spring Boot use to determine the desired response format?
Spring Boot uses the 'Accept' HTTP header sent by the client to decide the response content type.
Click to reveal answer
intermediate
How can you configure Spring Boot to support content type negotiation via URL path extensions?
You can enable path extension support by configuring a ContentNegotiationConfigurer in Spring Boot, allowing URLs like '/data.json' or '/data.xml' to specify the response format.
Click to reveal answer
intermediate
What role do HttpMessageConverters play in content type negotiation?
HttpMessageConverters convert Java objects to the requested format (like JSON or XML) and vice versa, enabling Spring Boot to produce the correct response type during content negotiation.
Click to reveal answer
beginner
What happens if the client requests a content type that the server cannot produce?
Spring Boot responds with HTTP status 406 Not Acceptable, indicating it cannot provide the requested content type.
Click to reveal answer
Which header does Spring Boot check to decide the response format?
AAuthorization
BContent-Type
CAccept
DUser-Agent
✗ Incorrect
The 'Accept' header tells the server what content types the client can handle.
What is the default response format if no content type is specified?
AJSON
BXML
CHTML
DPlain text
✗ Incorrect
Spring Boot defaults to JSON if no specific content type is requested.
How can clients specify the desired response format besides the Accept header?
AUsing the Content-Length header
BUsing cookies
CUsing the Host header
DUsing URL path extensions like .json or .xml
✗ Incorrect
URL path extensions can indicate the desired format if configured in Spring Boot.
What HTTP status code indicates the server cannot produce the requested content type?
A500 Internal Server Error
B406 Not Acceptable
C404 Not Found
D403 Forbidden
✗ Incorrect
406 Not Acceptable means the server cannot provide the requested content type.
Which Spring Boot component converts Java objects to JSON or XML?
AHttpMessageConverter
BController
CService
DRepository
✗ Incorrect
HttpMessageConverters handle converting Java objects to the requested response format.
Explain how Spring Boot decides which content type to send in a response.
Think about what the client sends and how Spring Boot matches it.
You got /4 concepts.
Describe what happens when a client requests a content type that Spring Boot cannot produce.
Consider how the server tells the client it can't fulfill the request.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of content type negotiation in Spring Boot?
easy
A. To send data in the format the client prefers
B. To secure the application from unauthorized access
C. To speed up database queries
D. To manage user sessions automatically
Solution
Step 1: Understand content type negotiation concept
Content type negotiation allows the server to send data in different formats like JSON or XML based on what the client requests.
Step 2: Identify the main purpose in Spring Boot
Spring Boot uses content type negotiation to automatically choose the response format that matches the client's preference.
Final Answer:
To send data in the format the client prefers -> Option A
Quick Check:
Content type negotiation = send preferred format [OK]
Hint: Remember: negotiation means choosing the client's format [OK]
Common Mistakes:
Confusing content type negotiation with security features
Thinking it speeds up database queries
Assuming it manages user sessions
2. Which of the following is the correct way to specify JSON response in a Spring Boot controller method?
easy
A. @GetMapping(value = "/data", consumes = "application/json")
B. @GetMapping(value = "/data", produces = "application/json")
C. @PostMapping(value = "/data", produces = "text/html")
D. @RequestMapping(value = "/data", method = GET, consumes = "text/plain")
Solution
Step 1: Understand 'produces' vs 'consumes'
'produces' defines the response content type; 'consumes' defines the request content type.
Step 2: Identify correct annotation for JSON response
@GetMapping with produces = "application/json" tells Spring Boot to send JSON data in response.
Final Answer:
@GetMapping(value = "/data", produces = "application/json") -> Option B
Quick Check:
produces = JSON means response is JSON [OK]
Hint: Use 'produces' to set response format [OK]
Common Mistakes:
Mixing 'consumes' with response content type
Using wrong HTTP method annotation
Setting produces to unrelated content types
3. Given this Spring Boot controller method:
@GetMapping(value = "/info", produces = {"application/json", "application/xml"})
public Data getInfo() {
return new Data("Spring", 5);
}
What will happen if the client sends an HTTP Accept header with value application/xml?
medium
A. The server responds with JSON format
B. The server ignores the Accept header and sends plain text
C. The server returns a 406 Not Acceptable error
D. The server responds with XML format
Solution
Step 1: Check the produces attribute
The method supports both JSON and XML response formats as per the produces array.
Step 2: Match client Accept header with produces
The client requests 'application/xml', which is supported, so Spring Boot will respond with XML.
Final Answer:
The server responds with XML format -> Option D
Quick Check:
Accept header matches produces = XML response [OK]
Hint: Response matches Accept header if supported [OK]
Common Mistakes:
Assuming JSON is always returned
Thinking 406 error occurs if multiple produces are set
Ignoring the Accept header
4. Consider this Spring Boot configuration snippet:
What is the likely problem if a client requests /api/data?format=txt?
medium
A. The server will throw an error due to unknown format
B. The server will respond with XML format
C. The server will respond with JSON as default
D. The server will ignore the format parameter and respond with plain text
Solution
Step 1: Analyze configured media types
Only 'json' and 'xml' are mapped to media types; 'txt' is not recognized.
Step 2: Check default content type behavior
If the parameter value is unknown, Spring Boot falls back to the default content type, which is JSON here.
Final Answer:
The server will respond with JSON as default -> Option C
Quick Check:
Unknown format uses default JSON [OK]
Hint: Unknown format falls back to default content type [OK]
Common Mistakes:
Expecting an error for unknown format
Assuming XML is default
Thinking plain text is sent automatically
5. You want to customize content negotiation in Spring Boot so that clients can request response format via URL parameter ?type= instead of the default ?format=. Which code snippet correctly achieves this?
hard
A. configurer.favorParameter(true).parameterName("type").defaultContentType(MediaType.APPLICATION_JSON);
B. configurer.favorParameter(false).parameterName("type").defaultContentType(MediaType.APPLICATION_JSON);
C. configurer.favorParameter(true).parameterName("format").defaultContentType(MediaType.APPLICATION_XML);
D. configurer.favorParameter(true).parameterName("type").defaultContentType(MediaType.TEXT_PLAIN);