Bird
0
0

Which of the following is the correct way to specify JSON response in a Spring Boot controller method?

easy📝 Syntax Q12 of 15
Spring Boot - Request and Response Handling
Which of the following is the correct way to specify JSON response in a Spring Boot controller method?
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")
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes