I have a requirement to set the different system property per each request to REST API.
Can System.setProperty("filePath", file.getAbsolutePath());
be used? is it thread safe? Will it override old value with new value when multiple/latest request comes to server before the current request is processed?

- 59
- 3
-
If it's per request, why would you want to set it at system level? – Federico klez Culloca Nov 23 '20 at 13:22
-
Why would you do that ... this is so unclear – Rabhi salim Nov 23 '20 at 13:22
-
i need to build a DefaultConfigurationBuilder which needs a configuration file where configuration file has this - fileName="${sys:filePath}" - filePath is dynamic and changes per each request. @Federico klez Culloca – Bharath Kumar Nov 23 '20 at 13:27
2 Answers
That very much depends on your exact setup. This here suggests for example that you can set different system properties in different websphere containers.
So, if you have such a setup, and if you can guarantee that only one client is using one endpoint at a time, this would theoretically be possible.
But the chain of ifs tells us the real answer: forget this idea. System properties aren't intended t be used like this!
If different users of that REST service need "one detail" to be different compared to other runs, then that absolutely implies: that needs to be a parameter that the clients pass to the server when invoking that REST service.

- 137,827
- 25
- 176
- 248
You are right, environment variables will be overrided by each System.setProperty
method call.
If you don't need to override there are plenty of solutions:
- Use servlet container context to store each request's file path
- Use Java ThreadLocal to manually store the property. But keep attention that you have to clean it up after request ends.

- 163
- 6