Spring Boot Building RESTful Services 5 — Questions and Answers
Question 1: What is the purpose of Spring HATEOAS in RESTful service development?
- Adding hypermedia links to REST responses so clients can discover actions dynamically (Correct answer)
- Securing REST endpoints with OAuth2
- Compressing REST responses for faster delivery
- Documenting REST APIs with OpenAPI specifications
Correct answer: Adding hypermedia links to REST responses so clients can discover actions dynamically
Spring HATEOAS provides tools to add hypermedia links (following the HATEOAS constraint of REST) to responses, enabling clients to navigate the API without hardcoded URLs.
Question 2: Which Spring Data REST annotation is used to prevent a repository method from being exported as a REST endpoint?
- @RestResource(exported = false) (Correct answer)
- @Hidden
- @NoExport
- @PrivateEndpoint
Correct answer: @RestResource(exported = false)
@RestResource(exported = false) on a repository method prevents Spring Data REST from automatically exposing that method as an HTTP endpoint.
Question 3: What does the produces attribute of @GetMapping do in a Spring REST controller?
- Restricts the endpoint to requests where the client's Accept header matches the specified media type (Correct answer)
- Sets the Content-Type of the request
- Generates response documentation
- Compresses the response body
Correct answer: Restricts the endpoint to requests where the client's Accept header matches the specified media type
The produces attribute narrows handler mapping so the method only handles requests whose Accept header is compatible with the specified media type(s).
Question 4: In a Spring Boot REST API, which property controls the base path for all Spring Data REST endpoints?
- spring.data.rest.base-path (Correct answer)
- server.rest.base-path
- spring.mvc.base-path
- spring.rest.root-path
Correct answer: spring.data.rest.base-path
Setting spring.data.rest.base-path in application.properties changes the root URL under which all Spring Data REST endpoints are registered.
Question 5: Which annotation on a Spring REST endpoint method tells Jackson to exclude null fields from the JSON response?
- @JsonInclude(JsonInclude.Include.NON_NULL) on the model class (Correct answer)
- @ExcludeNulls on the method
- @JsonIgnoreNulls on the controller
- @ResponseFilter(nulls=false) on the method
Correct answer: @JsonInclude(JsonInclude.Include.NON_NULL) on the model class
@JsonInclude(JsonInclude.Include.NON_NULL) on the DTO or model class instructs Jackson to omit any fields with null values from the serialized JSON output.
Question 6: What is the primary difference between @GetMapping and @RequestMapping(method = RequestMethod.GET) in Spring?
- They are functionally equivalent; @GetMapping is a shorthand composed annotation (Correct answer)
- @GetMapping only works on class level; @RequestMapping works on methods
- @RequestMapping supports wildcards but @GetMapping does not
- @GetMapping automatically enables caching; @RequestMapping does not
Correct answer: They are functionally equivalent; @GetMapping is a shorthand composed annotation
@GetMapping is a composed annotation introduced in Spring 4.3 as a shortcut for @RequestMapping(method = RequestMethod.GET); both are functionally identical.
Question 7: When using Spring's RestTemplate to consume an external REST API, which method should you use to send a POST request and receive a response body?
- postForObject() (Correct answer)
- getForEntity()
- exchange() with HttpMethod.GET
- delete()
Correct answer: postForObject()
RestTemplate.postForObject(url, request, ResponseType.class) sends an HTTP POST request and returns the deserialized response body directly.
What is the purpose of Spring HATEOAS in RESTful service development?