Spring Boot Building RESTful Services 2 — Questions and Answers
Question 1: Which annotation is used to map HTTP PATCH requests to handler methods in Spring MVC?
- @PatchMapping (Correct answer)
- @UpdateMapping
- @PartialMapping
- @ModifyMapping
Correct answer: @PatchMapping
@PatchMapping is the dedicated Spring MVC annotation for handling HTTP PATCH requests, used for partial resource updates.
Question 2: When a Spring REST controller method returns a ResponseEntity<Void> with HttpStatus.NO_CONTENT, what HTTP status code is sent to the client?
- 200
- 204 (Correct answer)
- 404
- 202
Correct answer: 204
HttpStatus.NO_CONTENT maps to HTTP 204, indicating the request succeeded but the response body is intentionally empty.
Question 3: What does the @RequestParam(required = false) annotation do in a Spring REST controller?
- Makes the query parameter optional with a null default (Correct answer)
- Throws an exception if the parameter is absent
- Maps the parameter to the request body
- Ignores the parameter entirely
Correct answer: Makes the query parameter optional with a null default
Setting required = false makes the query parameter optional; Spring injects null (or a specified defaultValue) when it is absent.
Question 4: Which Spring annotation allows you to handle exceptions globally across all @RestController classes?
- @RestControllerAdvice (Correct answer)
- @ExceptionMapper
- @GlobalExceptionHandler
- @ControllerFilter
Correct answer: @RestControllerAdvice
@RestControllerAdvice combines @ControllerAdvice and @ResponseBody to provide centralized exception handling with automatic JSON serialization.
Question 5: In Spring Boot, what is the default behavior when Jackson encounters an unknown JSON field during deserialization?
- Throws an exception
- Ignores the unknown field (Correct answer)
- Maps it to a generic Object
- Returns HTTP 400
Correct answer: Ignores the unknown field
By default, Spring Boot configures Jackson with DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES set to false, so unknown fields are silently ignored.
Question 6: What is the purpose of the @CrossOrigin annotation on a Spring REST controller?
- Enables CORS for the annotated controller or method (Correct answer)
- Encrypts cross-origin requests
- Redirects requests to another origin
- Blocks requests from other domains
Correct answer: Enables CORS for the annotated controller or method
@CrossOrigin configures Cross-Origin Resource Sharing (CORS) headers, allowing browsers to make requests from different origins to the annotated endpoint.
Question 7: Which method of UriComponentsBuilder is used to construct a URI for a newly created resource in a Spring REST response?
- fromCurrentRequest().path("/{id}").buildAndExpand(id).toUri() (Correct answer)
- newUri().append(id).build()
- buildUri(id).toString()
- createPath(id).toUri()
Correct answer: fromCurrentRequest().path("/{id}").buildAndExpand(id).toUri()
UriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(id).toUri() dynamically constructs the location URI from the current request context.
Which annotation is used to map HTTP PATCH requests to handler methods in Spring MVC?