Hi lately i've been studying about REST API. I was wondering how i can get all data if there is no parameter. I only can get the data by categoryId like below. http:// .... /categorys?categoryId=2 data is selected and displayed like this.
{
"items": [
{
"id": 2,
"name": "music",
"count": 20
}
]
}
But I also wanted to get all data without parameter like below => http:// .... /categorys
{
"items": [
{
"id": 1,
"name": "movie",
"count": 10
},
{
"id": 2,
"name": "music",
"count": 20
},
{
"id": 3,
"name": "book",
"count": 16
}
]
}
this is mysql query for this category data
SELECT_CATEGORYS = "SELECT category.id as id, category.name as name, count(*) as count FROM product inner join category where product.category_id = category.id group by category.id, category.name;"
and this is CategoryApiController.java
@RestController
@RequestMapping(path="/categorys")
public class CategoryApiController {
@Autowired
ReservationService reservationService;
@GetMapping
public Map<String, Object> categorys(@RequestParam(name="categoryId", required=false, defaultValue="0") int categoryId) {
List<Categorys> list = reservationService.getCategorys(categoryId);
Map<String, Object> map = new HashMap<>();
map.put("item", list);
return map;
}
}
please , help me solve this. thanks in advance. path should not change, I need to keep this getmapping /categorys so without parameter it should be /categorys and with parameter, it should be like this /categories?categoryId = 2