Good evening! I'm trying to learn how to implement microservice architectures with Spring Boot and I'm having trouble with some hard-coded paths in my projects.
There are 2 particular classes in my code where I have to put those paths manually, here they are:
DiscoverClientConfiguration.java : this class is made in order to connect via https to a Discovery Server, to implement it I've followed this tutorial, here the code:
System.setProperty("javax.net.ssl.keyStore", "C:\\ ..my file system path.. \\THIS PROJECT FOLDER\\src\\main\\resources\\keystore\\gateway.jks"); System.setProperty("javax.net.ssl.keyStorePassword", "**"); System.setProperty("javax.net.ssl.trustStore", "C:\\ ..my file system path.. \\THIS PROJECT FOLDER\\src\\main\\resources\\keystore\\gateway.jks"); System.setProperty("javax.net.ssl.trustStorePassword", "**");
RestTemplateConfiguration.java : this class is made in order to make a call to an external rest service using https, I've followed this tutorial, in order to implement it. Here the situation is even worse because I have to put the path to a .jks file in another project, here the code:
private HttpComponentsClientHttpRequestFactory validateSSL(){ String location = "C:\\ ..my file system path.. \\ANOTHER PROJECT FOLDER\\src\\main\\resources\\keystore\\be_device.jks"; String pass = "++"; ... some more code ... }
As you can imagine, everytime someone pulls the code he/she will have to change those paths which is not ideal.
In the first case I've tried to create a .properties
file to use the classpath:
notation, but it doesn't work and I get some exceptions at run time.
In the second case I've tried to use a notation of this kind
..\\..\\Centro\\Centro-Device\\src\\main\\resources\\keystore\\be_device.jks
in order to not write explicitly my file system path, but in my pc (Windows 10 professional) doesn't work at run time. On the other hand on the machine of another colleague, which uses Linux Mint, he said that this notation ../../Centro/Centro-Device/src/main/resources/keystore/be_device.jks
it works correctly(which I have tested on mine and doesn't work at run time).
Do you think that creating a Config Server using Spring Cloud could be a valuable solution for this problem? I was thinking of putting all of our file system paths in the Config Server and fetching them based on the machine in use. What do you think of this idea?
EDIT
Sorry if I didn't explain my self very well. My problem is not based on the fact of moving around the paths in some .properties, I want to have an environment where I can choose between multiple path options automatically because I've already tried the solutions with annotations like @Value. Almost like a DiscoveryServer but for file paths. The problem is that in this specific cases the software won't run correctly because it needs the whole file system path to the specified files.