Search code examples
jsonspring-bootpostmanrestget-mapping

field missing in the json response spring boot


I have Stock response class with only two fields as follows

class StockResponse {

private String orderId;
private String status;

//constructor

//getters and setters

}

and the also the controller as follows

@RestController
 @RequestMapping("/stocks")
 public class StockController {

 private static List<StockResponse> stocktList = new ArrayList <StockResponse > ();
 
 static {
     stocktList.add(new StockResponse("order1", "AVAILABLE"));
     stocktList.add(new StockResponse("order2", "AVAILABLE"));
     stocktList.add(new StockResponse("order3", "NOT AVAILABLE"));
     stocktList.add(new StockResponse("order4", "AVAILABLE"));
    
 }

 @GetMapping("/")
 public ResponseEntity < ? > getProsucts() {

  return ResponseEntity.ok(stocktList);

 }

 @GetMapping(path="/{id}", produces = "application/json;charset=UTF-8")
 public StockResponse getProsucts(@PathVariable String id) {

     StockResponse product = findOrder(id);
     
  if (product == null) {
 //  return ResponseEntity.badRequest(product)
 //   .body("Invalid product Id");
  }
  System.out.println(product.getOrderId());
  System.out.println(product.getStatus());
  

  return new StockResponse(product.getOrderId(), product.getStatus());

 }

 private StockResponse findOrder(String id) {
  return stocktList.stream()
   .filter(user -> user.getOrderId()
    .equals(id))
   .findFirst()
   .orElse(null);
 }


}

when I make a call to the localhost:8082/stocks/order1 I get a response with only one field showing up as follows enter image description here

what could I be missing out?


Solution

  • I ensured that all my getters and setters are public, I looked closely and I had missed public on

     public String getStatus()