Bird
Raised Fist0
Spring Bootframework~10 mins

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

Choose your learning style10 modes available
Concept Flow - Content type negotiation
Client sends HTTP request
↓
Server checks Accept header
↓
Match supported content types?
No→Respond 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.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To send data in the format the client prefers -> Option A
  4. 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

  1. Step 1: Understand 'produces' vs 'consumes'

    'produces' defines the response content type; 'consumes' defines the request content type.
  2. Step 2: Identify correct annotation for JSON response

    @GetMapping with produces = "application/json" tells Spring Boot to send JSON data in response.
  3. Final Answer:

    @GetMapping(value = "/data", produces = "application/json") -> Option B
  4. 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

  1. Step 1: Check the produces attribute

    The method supports both JSON and XML response formats as per the produces array.
  2. Step 2: Match client Accept header with produces

    The client requests 'application/xml', which is supported, so Spring Boot will respond with XML.
  3. Final Answer:

    The server responds with XML format -> Option D
  4. 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:
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorParameter(true)
              .parameterName("format")
              .defaultContentType(MediaType.APPLICATION_JSON)
              .mediaType("xml", MediaType.APPLICATION_XML)
              .mediaType("json", MediaType.APPLICATION_JSON);
}
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

  1. Step 1: Analyze configured media types

    Only 'json' and 'xml' are mapped to media types; 'txt' is not recognized.
  2. 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.
  3. Final Answer:

    The server will respond with JSON as default -> Option C
  4. 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);

Solution

  1. Step 1: Enable parameter-based content negotiation

    Set favorParameter(true) to allow URL parameter control.
  2. Step 2: Change parameter name to 'type'

    Use parameterName("type") to listen for '?type=' instead of default '?format='.
  3. Step 3: Set default content type to JSON

    defaultContentType(MediaType.APPLICATION_JSON) ensures JSON if no parameter is given.
  4. Final Answer:

    configurer.favorParameter(true).parameterName("type").defaultContentType(MediaType.APPLICATION_JSON); -> Option A
  5. Quick Check:

    favorParameter + parameterName = custom URL param [OK]
Hint: Set favorParameter true and parameterName to customize URL param [OK]
Common Mistakes:
  • Disabling favorParameter disables URL param negotiation
  • Using wrong parameter name
  • Setting inappropriate default content type
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard