0
0
Spring Bootframework~10 mins

Content type negotiation in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Content type negotiation
Client sends HTTP request
Server checks Accept header
Match supported content types?
NoRespond 406 Not Acceptable
Yes
Select best content type
Serialize response data accordingly
Send HTTP response with Content-Type header
The server reads the client's Accept header, chooses a supported content type, serializes the response, and sends it back with the correct Content-Type.
Execution Sample
Spring Boot
  @GetMapping(value = "/data", produces = {"application/json", "application/xml"})
  public Data getData() {
      return new Data("Hello", 123);
  }
This Spring Boot controller method returns data in JSON or XML depending on the client's Accept header.
Execution Table
StepClient Accept HeaderServer ActionSelected Content-TypeResponse Body Format
1application/jsonCheck if application/json supportedapplication/jsonJSON serialized data
2application/xmlCheck if application/xml supportedapplication/xmlXML serialized data
3text/htmlNo matching supported typeNoneRespond 406 Not Acceptable
💡 Execution stops when a matching content type is found or 406 error is returned if none match.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
Accept HeaderNoneapplication/jsonapplication/xmltext/html
Selected Content-TypeNoneapplication/jsonapplication/xmlNone
Response StatusNone200 OK200 OK406 Not Acceptable
Key Moments - 2 Insights
Why does the server respond with 406 Not Acceptable sometimes?
Because the client's Accept header requests a content type the server does not support, as shown in step 3 of the execution_table.
How does the server decide which content type to use when multiple are supported?
The server matches the client's Accept header in order and picks the first supported type, as seen in steps 1 and 2 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what content type is selected when the client sends 'application/xml'?
Atext/html
Bapplication/json
Capplication/xml
D406 Not Acceptable
💡 Hint
Check row 2 under 'Selected Content-Type' in the execution_table.
At which step does the server respond with 406 Not Acceptable?
AStep 3
BStep 1
CStep 2
DNever
💡 Hint
Look at the 'Response Status' column in variable_tracker after Step 3.
If the client sends 'application/json, application/xml', which content type will the server choose?
Aapplication/xml
Bapplication/json
Ctext/html
D406 Not Acceptable
💡 Hint
Server picks the first supported type matching the Accept header, see execution_table steps 1 and 2.
Concept Snapshot
Content type negotiation in Spring Boot:
- Client sends Accept header with preferred types
- Server checks supported types in @GetMapping produces
- Matches Accept header to supported types
- Serializes response accordingly
- Sends response with matching Content-Type
- If no match, responds 406 Not Acceptable
Full Transcript
Content type negotiation is how a Spring Boot server decides what format to send data in. The client sends an HTTP request with an Accept header listing preferred content types like JSON or XML. The server looks at this header and compares it to the types it can produce, defined in the @GetMapping annotation's produces attribute. If the server supports one of the requested types, it serializes the response data in that format and sends it back with the correct Content-Type header. If none match, the server responds with a 406 Not Acceptable status. This process ensures the client gets data in a format it understands.