'How to read .yml file according to a passed parameter and how to set a default value if parameter does not match in Springboot?
Is there a way I can pass a parameter to specify which list in "APP" / "WEB" I like to get? e.g. getting [abc.com, efg.com] when I pass "APP" as parameter. And can I have a Default result when none of them is matched against that parameter? Thank you.
I get the .yml file as Map with below code
url:
filter:
APP:
- abc.com
- def.com
...
WEB:
- ghi.com
- jkl.com
/*DEFAULT?*/
I only want to get the list inside "APP" or "WEB" according to the parameter passed if there can be any. But right now I have to get the whole filter as map before I get the list I want. And I don't know how to make a Default list if parameter is not "APP" nor "WEB"
@ConfigurationProperties(prefix="url")
public class UrlConfig {
private Map<String,List<String>>filter;
public Map<String, List<String>> getFilter() {
return filter;
}
public List<String> getFilterByDevice(String device){
return getFilter().get(device);
}
}
In getFilter(), I get the result as below. By getFilterByDevice(device), I get the List according to the device but I have to get the whole map in getFilter() first. Is there a way not to get the whole map before getting the list I want?
{APP=[abc.com, def.com], WEB=[ghi.com, jkl.com]}
This is where I get the value, device has to be read from a method:
private void filter(device){
List<String> filter = UrlConfig.getFilterByDevice(device);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
