-4

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?

2 Answers2

0

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.


GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

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:

  1. Use servlet container context to store each request's file path
  2. Use Java ThreadLocal to manually store the property. But keep attention that you have to clean it up after request ends.