Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring application.yaml have dash in key

I want to create @Bean ExampleProperties.java and want to automatically get key/value in application.yaml, for example:

@ConfigurationProperties("server")
public class ExampleProperties {
    private String context-path; //this would be a mistake, cause you could not have "-" in parameter name,
}

How to create a parameter equals to the key in application.yml??

like image 584
leo Avatar asked Sep 12 '25 07:09

leo


2 Answers

According to Spring documentation property with dash would be represented in java as

@ConfigurationProperties("server")
public class ExampleProperties {
  private String contextPath;
}
like image 164
kqr Avatar answered Sep 15 '25 00:09

kqr


If you are like me and your property looks like this :

my-config-map.key

Then you want:

@ConfigurationProperties(prefix = "my-config-map")
public class Prop {

      private String key;
  
      // getter/setter

}
like image 27
Eugene Avatar answered Sep 15 '25 01:09

Eugene