0
0
Spring Bootframework~20 mins

Content type negotiation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Content Type Negotiation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the response content type when client requests JSON?

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?

Spring Boot
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) {}
AThe server responds with Content-Type: application/xml and XML formatted data.
BThe server responds with Content-Type: application/octet-stream and binary data.
CThe server responds with Content-Type: text/plain and plain text data.
DThe server responds with Content-Type: application/json and JSON formatted data.
Attempts:
2 left
💡 Hint

Check the Accept header and the produces attribute in the controller.

📝 Syntax
intermediate
2:00remaining
Which annotation correctly specifies multiple content types for response?

In Spring Boot, which of the following @GetMapping annotations correctly specifies that the method can produce both JSON and XML responses?

A@GetMapping(value = "/info", produces = "application/json, application/xml")
B@GetMapping(value = "/info", produces = {"application/json", "application/xml"})
C@GetMapping(value = "/info", produces = "application/json|application/xml")
D@GetMapping(value = "/info", produces = ["application/json", "application/xml"])
Attempts:
2 left
💡 Hint

Look at the correct Java array syntax for the produces attribute.

🔧 Debug
advanced
2:00remaining
Why does the server respond with 406 Not Acceptable?

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?

Spring Boot
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) {}
AThe server is down and returns 406 as a default error.
BThe server has a syntax error in the controller method causing 406.
CThe server cannot produce the requested content type application/pdf, so it returns 406 Not Acceptable.
DThe client request is missing an Authorization header, causing 406.
Attempts:
2 left
💡 Hint

Think about what 406 status means in HTTP.

state_output
advanced
2:00remaining
What is the response content type if no Accept header is sent?

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?

Spring Boot
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) {}
AThe server responds with Content-Type: application/json by default.
BThe server responds with Content-Type: application/xml by default.
CThe server responds with Content-Type: text/html by default.
DThe server responds with 406 Not Acceptable error.
Attempts:
2 left
💡 Hint

Think about the default content type Spring Boot uses when no Accept header is specified.

🧠 Conceptual
expert
3:00remaining
How does Spring Boot determine response content type with multiple converters?

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?

ASpring Boot matches the client's Accept header with the supported media types of converters and picks the best match.
BSpring Boot always uses the JSON converter if available, ignoring the Accept header.
CSpring Boot uses the first registered converter regardless of the Accept header.
DSpring Boot randomly picks a converter from the registered list.
Attempts:
2 left
💡 Hint

Consider how HTTP content negotiation works with Accept headers and converters.