I have a standard BREAD controller where all operations URLs are as follow
| Operation | Method | function | URL |
|---|---|---|---|
| Browse | GET | index | /api/levels/{levelId}/items |
| Read | GET | show | /api/levels/{levelId}/items/{id} |
| Edit | PUT | update | /api/levels/{levelId}/items/{id} |
| Add | POST | create | /api/levels/{levelId}/items |
| Delete | DELETE | delete | /api/levels/{levelId}/items/{id} |
I have my ItemController
@RestController
@RequestMapping("/api/levels")
@RequiredArgsConstructor
public class ItemController {
/*...*/
@PutMapping("/{levelId}/items/{id}")
public void update(@Valid @RequestBody ItemRequest request, @PathVariable Long levelId, @PathVariable Long id) {
service.update(request, id, levelId);
}
/*...*/
}
Is it possible to set the levelId path varaible inside the controller request mapping like this
@RestController
@RequestMapping("/api/levels/{levelId}/items")
@RequiredArgsConstructor
public class ItemController {
/*...*/
@PutMapping("/{id}")
public void update(@Valid @RequestBody ItemRequest request, @PathVariable Long levelId, @PathVariable Long id) {
service.update(request, id, levelId);
}
/*...*/
}
>Solution :
@RequestMapping("/api/levels/{levelId}/items")
Yes, it is possible, and this should work.
If you are seeing an error while running this, edit your post and add it.