0

I'm mapping an endpoint to fetch an object by it's ID from an array of Objects

this is my request mapping:

@RequestMapping(value ="/items/{Id}", method = RequestMethod.GET) 
public Item getItemById(@PathVariable("Id") String Id) throws NoSuchItem {
return ItemList.getItemById(Id);
}

this is my getItemById function in my ItemList class,

 private static ArrayList<Item> itemList;

 public static Item getItemById(String id) throws NoSuchItem {
    for(Item x:itemList){
         if(x.getId()== id)
          return x;
     }
     throw new NoSuchItem();
 }

this is the JSON representation of my itemList array which has 3 Item objects,

[
{
    "name": "Mars",
    "supplier": "Nestle",
    "weight": "1",
    "id": "mars",
    "location": null
},
{
    "name": "Kit Kat",
    "supplier": "Nestle",
    "weight": "1",
    "id": "kitkat",
    "location": null
},
{
    "name": "Double Decker",
    "supplier": "Nestle",
    "weight": "1",
    "id": "dd",
    "location": null
}
]

I want my /items/{id} endpoint to return only the desired Item, But it throws a 404 error, what am I doing wrong here?

PS- I'm sending my requests through POSTMAN

the cURl command for my postman request is as follows;

curl --location --request GET 'localhost:8082/items/mars'
Majid Roustaei
  • 1,556
  • 1
  • 20
  • 39

0 Answers0