Skip to content

[fix]: Null Handling for getStockById #3

@0PrashantYadav0

Description

@0PrashantYadav0

Description

  • Detailed Description:
    The method getStockById in OperationController returns null when no stock is found, which is not an informative response for API consumers. A proper HTTP 404 Not Found should be returned.
  • How to Solve:
    1. Use ResponseEntity:
      Modify the method to return a ResponseEntity so that you can control the HTTP status.
    2. Check for Null:
      If the stock is null, return a 404 response:
      @GetMapping("/{id}")
      public ResponseEntity<Stock> getStockById(@PathVariable Long id) {
          Stock stock = operationService.getStockById(id);
          return (stock != null) ? ResponseEntity.ok(stock) : ResponseEntity.notFound().build();
      }
    3. Test the Endpoint:
      Use tools like Postman or cURL to ensure that a non-existent stock ID returns a 404 status.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions