Ferdi K
Ferdi K

Reputation: 31

Spring boot function stack context

Is there any way I can create, lets say a Context, and depending on the function stack know in what context I am in and retrieve values out of it?

e.g.:

Request comes into Controller (Contains requestId in header). Controller calls function from Service1. Service1 calls function from Service2.

Controller -1-> Service1 -2-> Service2
    ^            |  ^            |
    |            |  |            |
    +---return---+  +---return---+

If I need the requestId in Service2, i have to pass it all the way down manually. But it would be way more convenient to pass it via a "function stack context" (or whatever).

Thank you in advance

Upvotes: 1

Views: 41

Answers (1)

Alejandro Venegas
Alejandro Venegas

Reputation: 249

The better way is pass the variable all the way down, other ways are passing through session, cookies, context, etc.. but that is ugly because broke the unique responsability of the services.

Upvotes: 0

Related Questions