Essential Spring Boot REST API Fundamentals Quiz Quiz

Explore key aspects of building REST APIs using Spring Boot. This quiz evaluates your understanding of RESTful architecture concepts, Spring annotations, HTTP methods, and common practices for developing Spring Boot-based APIs.

  1. Identifying the Correct HTTP Method

    Which HTTP method should be used in a Spring Boot REST API to update an existing resource, such as changing a user's email address?

    1. PUT
    2. DELETE
    3. GET
    4. POST

    Explanation: The PUT method is used to update existing resources in RESTful systems, which includes modifying a user's details. POST is intended for creating new resources, not updates. GET is used for retrieving data, not making changes. DELETE is for removing resources, not modifying them.

  2. Annotation for Routing HTTP Requests

    What annotation is commonly used on a method in a Spring Boot controller to map it to an HTTP GET request, such as retrieving all books from a library?

    1. @PutMapping
    2. @PostMapping
    3. @GetMapping
    4. @DeleteMapping

    Explanation: @GetMapping is used to map HTTP GET requests, which are typically used for retrieving data like a list of books. @PostMapping maps POST requests, which are used to create resources. @PutMapping is for updating, and @DeleteMapping is for deleting resources, making them inappropriate choices here.

  3. Defining a REST Controller

    Which annotation should you use to mark a class as a REST controller in a Spring Boot application?

    1. @RestController
    2. @Repository
    3. @Component
    4. @Service

    Explanation: @RestController designates a class as the controller that handles REST API requests, ensuring responses are sent as JSON or XML. @Service is used for service classes, not for request handling. @Repository relates to data access, while @Component is a generic stereotype without specific REST features.

  4. Returning an HTTP Status Code

    What is the standard HTTP status code to return when a new resource is successfully created via a POST request in a Spring Boot REST API?

    1. 400 Bad Request
    2. 204 No Content
    3. 200 OK
    4. 201 Created

    Explanation: 201 Created is the correct status code to indicate a resource has been created as the result of a POST request. 200 OK means a successful generic operation, not necessarily resource creation. 204 No Content indicates success with no response body. 400 Bad Request signals a problem with the request, not a successful operation.

  5. Reading Path Variables

    How do you capture a dynamic value from a URL path, such as '/users/{id}', in a Spring Boot REST controller method?

    1. @RequestBody
    2. @PathVariable
    3. @Value
    4. @RequestParam

    Explanation: @PathVariable extracts values from the URL path, such as an ID in '/users/{id}'. @RequestBody is used for values in the request body. @RequestParam handles query parameters, not path variables. @Value is for property values, not HTTP request data.

  6. Accepting JSON Payloads

    Which annotation allows a method parameter in a Spring Boot controller to bind to a JSON object sent in the request body, for example, creating a new product?

    1. @RequestParam
    2. @RequestBody
    3. @ControllerAdvice
    4. @Autowired

    Explanation: @RequestBody is used to bind a JSON request body to a Java object, which is useful when creating new resources. @RequestParam is suitable for query parameters, not JSON bodies. @ControllerAdvice is for global exception handling, and @Autowired is used for dependency injection, making them both incorrect here.

  7. Disabling Cross-Origin Restrictions

    Which annotation can you use in Spring Boot to enable Cross-Origin Resource Sharing (CORS) for a specific controller or method?

    1. @EnableCors
    2. @CorsAllowed
    3. @CrossOrigin
    4. @AllowOrigin

    Explanation: @CrossOrigin enables CORS for controllers or individual methods, allowing cross-origin requests. @CorsAllowed and @AllowOrigin are not standard annotations. @EnableCors does not exist as an annotation in this context, so those options are incorrect.

  8. Returning a List of Objects

    If you want your Spring Boot REST API to return a list of user objects as JSON when clients send a GET request, what should your controller method return?

    1. A List of user objects
    2. An integer count of users
    3. A single user object
    4. A string representing user names

    Explanation: Returning a List of user objects allows the API to provide all users as a JSON array in the response. Returning a single user object or an integer does not provide the full list. Returning only a string gives less structured and less useful data for clients.

  9. Handling Resource Not Found

    Which HTTP status code should your API return if a client requests a resource by ID that does not exist, like '/orders/999' where order 999 is not found?

    1. 404 Not Found
    2. 200 OK
    3. 500 Internal Server Error
    4. 403 Forbidden

    Explanation: 404 Not Found is used to indicate that the requested resource does not exist. 200 OK would incorrectly signal a successful retrieval. 403 Forbidden means the client is not allowed to access the resource. 500 Internal Server Error is for server faults, not missing data.

  10. Specifying API Base Paths

    Which annotation is used to map the base path of a REST API in a Spring Boot controller, such as '/api/products'?

    1. @RequestMapping
    2. @ApiController
    3. @PathVariable
    4. @BasePath

    Explanation: @RequestMapping can be placed at the class level to define the base URL for all methods within the controller. @BasePath and @ApiController are not standard annotations. @PathVariable handles path values, not base path mappings.