Spring Boot Building RESTful Services 3 — Questions and Answers
Question 1: What is the role of the @PathVariable annotation in Spring REST controllers?
- Binds a URI template variable to a method parameter (Correct answer)
- Maps the entire request path to a String
- Reads a value from the request header
- Injects the servlet path info
Correct answer: Binds a URI template variable to a method parameter
@PathVariable extracts values from URI template placeholders (e.g., /users/{id}) and binds them to method parameters.
Question 2: Which Spring Boot property disables the default error whitelabel page for REST APIs?
- server.error.whitelabel.enabled=false (Correct answer)
- spring.mvc.error.page=false
- spring.boot.error.enabled=false
- server.error.page.enabled=false
Correct answer: server.error.whitelabel.enabled=false
Setting server.error.whitelabel.enabled=false in application.properties disables the default HTML error page, allowing custom error handling.
Question 3: What HTTP status code should a REST API return when a client sends a request with an unsupported Content-Type?
- 415 Unsupported Media Type (Correct answer)
- 400 Bad Request
- 406 Not Acceptable
- 422 Unprocessable Entity
Correct answer: 415 Unsupported Media Type
HTTP 415 Unsupported Media Type indicates that the server cannot process the request's payload format as specified by the Content-Type header.
Question 4: In a Spring REST API, what annotation would you use to validate that a request body field is not blank?
- @NotBlank (Correct answer)
- @NotEmpty
- @NonNull
- @Required
Correct answer: @NotBlank
@NotBlank (from javax.validation) ensures the field is not null and contains at least one non-whitespace character, failing if it's an empty or whitespace-only string.
Question 5: Which interface should a custom Spring REST error response class implement to integrate with Spring's default error handling mechanism?
- ErrorResponse (Correct answer)
- ResponseBodyAdvice
- HandlerExceptionResolver
- ErrorAttributes
Correct answer: ErrorResponse
Implementing ErrorResponse allows a custom exception to carry RFC 9457 Problem Details fields, which Spring's exception handlers can serialize automatically.
Question 6: What is the effect of annotating a Spring @RestController method with @ResponseStatus(HttpStatus.CREATED)?
- Sets the HTTP response status to 201 Created automatically (Correct answer)
- Throws an exception with status 201
- Requires the method to return a ResponseEntity
- Creates a new HTTP session
Correct answer: Sets the HTTP response status to 201 Created automatically
@ResponseStatus(HttpStatus.CREATED) instructs Spring to set the response status to 201 Created without needing to wrap the return value in ResponseEntity.
Question 7: Which Spring MVC component is responsible for converting Java objects to JSON in REST responses?
- HttpMessageConverter (Correct answer)
- ViewResolver
- HandlerAdapter
- ContentNegotiationManager
Correct answer: HttpMessageConverter
HttpMessageConverter implementations (specifically MappingJackson2HttpMessageConverter) serialize Java objects to JSON and deserialize JSON to Java objects.
What is the role of the @PathVariable annotation in Spring REST controllers?