1

So my application.yml would be something like this:

urls:
  login: http://mylogin.url
restInterceptor:
  log:
    fieldsToHide: {'${urls.login}':'password'}

And I'd need this in my java code:

private Map<String, String> fieldsToHide;

First I tried with the @Value annotation, like this:

@Value("${restInterceptor.log.fieldsToHide}")
private Map<String, String> fieldsToHide;

But it fails. It seems that you cannot inject a map from a yaml with the @Value annotation, so I tried the solution in this question.

@Service
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "restInterceptor.log")
public class MyClass {

  private Map<String, String> fieldsToHide;

And then the map gets injected, but the key's value is '${urls.login}' instead of http://mylogin.url.

If I configure the map in my yaml the other way round {'password':'${urls.login}'}, it gets correctly injected with the property value.

What am I doing wrong? Is this even possible?

gualizoe
  • 159
  • 1
  • 1
  • 12
  • Map have key value pair so in your first case expression not evaluate '${urls.login}' because it understand as key but in second evaluate – DHARMENDRA SINGH Sep 06 '18 at 10:48
  • Yes Dharmendra, I assumed that. But why? And how can I get it evaluated if it's a key? I know it can be done with a properties file, but I'm not able with the yaml. That's my question :) – gualizoe Sep 06 '18 at 14:06
  • have you tried with Environment like @Autowired private Environment env; and call using env.getProperty("fieldsToHide"); – DHARMENDRA SINGH Sep 13 '18 at 12:03

0 Answers0