Reuse controller paths for a Spring Boot Controller

37 Views Asked by At

We have a Person class and in our Spring boot Controller for the Person class we have a few functions that have a path that starts with

/person/{id}

However, we want to reuse all those functions for other paths where a Person Object can be retrieved like for example:

/payment/{id}/payer
/payment/{id}/receiver

Otherwise, we need to write the functionality for GetMapping(path = "[...]/first-name") a lot of times. Is there a way to achieve this?

To clarify: For these three URL's it should execute:

// person = the Person Object that would normally be returned
return person.getFirstName();
1

There are 1 best solutions below

1
Nguyen Tran Nhat Vu On

You can try this

@GetMapping(path = {"/person/{id}", "/payment/{id}/payer", "/payment/{id}/receiver"})