Spring Boot Building RESTful Services 4 — Questions and Answers
Question 1: What is the purpose of content negotiation in a Spring REST API?
- Selecting the response format based on the client's Accept header (Correct answer)
- Encrypting the response body
- Balancing load between multiple REST endpoints
- Caching responses based on content type
Correct answer: Selecting the response format based on the client's Accept header
Content negotiation allows the server to serve different representations (JSON, XML, etc.) of the same resource based on the client's Accept header.
Question 2: In Spring Boot, what annotation combination makes a class a REST controller that also serves as a Spring component?
- @RestController (Correct answer)
- @Controller + @Service
- @Component + @ResponseBody
- @Bean + @RequestMapping
Correct answer: @RestController
@RestController is a composed annotation that combines @Controller (component stereotype) and @ResponseBody, registering the class as a bean and enabling automatic response serialization.
Question 3: What does Spring's @RequestBody annotation do when used on a controller method parameter?
- Deserializes the HTTP request body into a Java object (Correct answer)
- Reads the first query parameter from the URL
- Maps the URL path to a Java object
- Injects the raw HttpServletRequest
Correct answer: Deserializes the HTTP request body into a Java object
@RequestBody instructs Spring to read the HTTP request body and deserialize it into the annotated parameter type using the configured HttpMessageConverter.
Question 4: Which HTTP method is idempotent but NOT safe, meaning it can modify server state but repeated calls produce the same result?
- PUT (Correct answer)
- POST
- PATCH
- DELETE
Correct answer: PUT
PUT is idempotent because calling it multiple times with the same payload results in the same server state, but it is not safe because it modifies the resource.
Question 5: What happens when you annotate a @RestController method parameter with @Valid and the validation fails?
- Spring throws MethodArgumentNotValidException and returns HTTP 400 (Correct answer)
- Spring silently ignores validation errors
- Spring throws a NullPointerException
- Spring returns HTTP 500
Correct answer: Spring throws MethodArgumentNotValidException and returns HTTP 400
When bean validation fails on a @RequestBody annotated with @Valid, Spring throws MethodArgumentNotValidException, which by default maps to a 400 Bad Request response.
Question 6: Which Spring Boot feature automatically registers an endpoint at /actuator/health for REST APIs?
- Spring Boot Actuator (Correct answer)
- Spring Boot DevTools
- Spring Data REST
- Spring Boot Admin
Correct answer: Spring Boot Actuator
Spring Boot Actuator provides production-ready endpoints including /actuator/health, which reports the application's health status.
Question 7: In a Spring REST API, what is the correct way to return a 404 Not Found response when a resource does not exist?
- Return ResponseEntity.notFound().build() (Correct answer)
- Throw a RuntimeException
- Return null from the controller method
- Use @ResponseStatus(HttpStatus.OK) with an empty body
Correct answer: Return ResponseEntity.notFound().build()
ResponseEntity.notFound().build() creates a ResponseEntity with HTTP 404 status and no body, which is the idiomatic Spring way to signal a missing resource.
What is the purpose of content negotiation in a Spring REST API?