Reputation: 21
I have a class that stores the different endpoints for an API. The class looks like the following:
public class APIEndpoints {
public static String LOG_IN = "/api/auth/login";
public static String LOG_OUT= "/api/auth/logout";
public static String GET_INSTANCE ="/api/{objectID}/instances?offset={offset}&limit{limit}";
public static String getInstance(String reportID, int offSet, int limit){
return GET_INSTANCE.replace("{reportID}",reportID)
.replace("{offset}", String.valueOf(offSet))
.replace("{limit}", String.valueOf(limit));
}
}
I would like that the endpoints URLs ("api/auth/login" for example) , are loaded from a file, like a endpoints.properties.
I'm using SpringBoot, but it does not allow to inject values on static variables.
What would be the 'most' elegant solution to solve this? How would you approach it?
Thank you.
Upvotes: 0
Views: 655
Reputation: 41
Create a small function which would get the values from the property file and assign it to the static LOG_IN
variable:
private static String LOG_IN;
@Value("${LOG_IN}")
public void setLoginUrl(String LOG_IN) {
try {
APIEndpoints.LOG_IN= LOG_IN;
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0
Reputation: 203
The question is already answered: link
Static @Value fields are not recommended, but here is how you can do it:
@Value("${url}")
public void setUrl(String url) {
APIEndpoints.url = url;
}
Upvotes: 1
Reputation: 561
You can access it by using @Value
Annotation as below
@Value("${your.property.name}")
private String property;
and in your endpoints.properties
file you have to define It like
your.property.name=propertyValue
Upvotes: 1