Bird
Raised Fist0
Spring Bootframework~10 mins

Content type negotiation in Spring Boot - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the content type produced by the controller method.

Spring Boot
@GetMapping(value = "/data", produces = "[1]")
public String getData() {
    return "Hello";
}
Drag options to blanks, or click blank then click option'
Aapplication/json
Btext/plain
Capplication/xml
Dtext/html
Attempts:
3 left
💡 Hint
Common Mistakes
Using a content type that does not match the returned data format.
Forgetting to specify the produces attribute.
2fill in blank
medium

Complete the code to accept JSON input in the controller method.

Spring Boot
@PostMapping(value = "/submit", consumes = "[1]")
public ResponseEntity<String> submitData(@RequestBody Data data) {
    return ResponseEntity.ok("Received");
}
Drag options to blanks, or click blank then click option'
Atext/plain
Bapplication/xml
Cmultipart/form-data
Dapplication/json
Attempts:
3 left
💡 Hint
Common Mistakes
Using text/plain instead of application/json.
Not specifying the consumes attribute when expecting JSON.
3fill in blank
hard

Fix the error in the annotation to correctly produce XML content.

Spring Boot
@GetMapping(value = "/info", produces = "[1]")
public Info getInfo() {
    return new Info("data");
}
Drag options to blanks, or click blank then click option'
Aapplication/json
Btext/html
Capplication/xml
Dtext/plain
Attempts:
3 left
💡 Hint
Common Mistakes
Using application/json when XML is expected.
Not matching the produces type with the returned data format.
4fill in blank
hard

Fill both blanks to accept JSON input and produce JSON output.

Spring Boot
@PostMapping(value = "/process", consumes = "[1]", produces = "[2]")
public ResponseEntity<String> process(@RequestBody Input input) {
    return ResponseEntity.ok("Done");
}
Drag options to blanks, or click blank then click option'
Aapplication/json
Btext/plain
Capplication/xml
Dtext/html
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing content types between consumes and produces.
Using plain text or XML instead of JSON.
5fill in blank
hard

Fill all three blanks to create a controller method that accepts XML, produces JSON, and maps to "/convert".

Spring Boot
@RequestMapping(value = "/[1]", consumes = "[2]", produces = "[3]")
public ResponseEntity<String> convert(@RequestBody Data data) {
    return ResponseEntity.ok("Converted");
}
Drag options to blanks, or click blank then click option'
Aconvert
Bapplication/json
Capplication/xml
Dsubmit
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping consumes and produces media types.
Using incorrect URL path in the value attribute.

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