Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to put a path varaible inside the controllers request mapping

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading