Spring Boot Interview QA Series Part 4

Jul 4, 2024

Java Tii: Spring Boot Interview QA Series Part 4

Introduction

  • Continuation of the Spring Boot Interview QA series.
  • Emphasis on practical implementations in RESTful web services.

RESTful Web Services & HTTP Methods

  • HTTP Methods Used: Post, Put, Get, Patch, Delete.
    • Post: Create a resource.
    • Put: Update a resource.
    • Get: Retrieve a resource.
    • Patch: Partially update a resource.
    • Delete: Remove a resource.

Specifying HTTP Methods

  • Annotations in Spring framework for HTTP methods:
    • Post Mapping: For inserting.
    • Put Mapping: For updating.
    • Get Mapping: For retrieving.
    • Patch Mapping: For partial updates.
    • Delete Mapping: For removing resources.
    • @RequestBody: Passing objects in Post and Put.
    • URL approach for Get and Delete.

RequestMapping & Specific Annotations

  • @RequestMapping: Parent annotation that other HTTP methods extend from.
  • Usage example for RequestMethod.

Designing REST Endpoints

Scenario 1:

  • Design an endpoint to filter products by type using @GetMapping and @PathVariable.
  • Example: /products/search/{productType}

Scenario 2:

  • Design an endpoint with optional filtering using @GetMapping and @RequestParam. Returns all products if no filter is provided.
  • Example: /filter?productType={type}

@PathVariable vs. @RequestParam

  • @PathVariable: Mandatory input; part of URL.
  • @RequestParam: Optional input; part of URL parameters.

RestController vs Controller

  • @RestController: Simplified handling for returns (JSON/XML directly).
  • @Controller: Requires @ResponseBody for non-view returns.

Deserializing JSON Request Payload

  • Use @RequestBody to map JSON to a Java object.
  • Example with Book and Author classes.

Updating with Post vs. Put

  • Mix of update and insert should follow REST principles.
  • Idempotency: Post (non-idempotent), Put (idempotent).

Request Body in Get Method

  • Technically possible but not recommended.
  • Get should use URL parameters.

Content Negotiation

  • Enable to support multiple media types (JSON, XML) using @Produces and config properties.
  • Example configuration entries in application.properties.

Common HTTP Status Codes

  • 4xx: Client errors (400, 404, 401, 403, 405, 415).
  • 5xx: Server errors (500, 502).
  • 2xx: Success (200, 201).

Customizing Status Codes

  • Use @ResponseStatus for custom HTTP responses.

Enabling Cross-Origin (CORS)

  • @CrossOrigin: Annotation or using Java-based config for allowed origins/hosts/ports.

File Upload in Spring Boot

  • Use MultipartFile data type.
  • Example: File upload handler in a controller.

API Versioning

  • Strategies: URI path, request parameters, headers.
  • Example implementations for FlightBookingController.

API Documentation

  • Use Swagger (Open API 3) for auto-documentation.
  • Example dependencies and config entries.

Hiding Endpoints in Documentation

  • @Hidden: Annotation to hide specific endpoints in Swagger.

Consuming REST APIs

  • Approaches: RestTemplate, FeignClient, WebClient, Advanced Rest Client.
  • Examples for each with dependencies and usage instructions.

Conclusion

  • Inviting audience to engage and ask questions.