0
0
Spring Bootframework~10 mins

Content type negotiation in Spring Boot - Interactive Code Practice

Choose your learning style9 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.