DachuanZhao
DachuanZhao

Reputation: 1339

How to get yml value when the key contains underscore in springboot2.5.2?

My application.yml is

app:
  ranking:
    UNK: "[UNK]"
    "ARTICLE_FEATURE_GRPC_API": "0.0.0.0:50055"
    "ARTICLE_STABLE_MODEL_NAME": "article_push_stable"
    "PREDICT_BATCH_SIZE": 256

And my config class is :

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app.ranking")
public class RankingConfig {
    public static String UNK;
    public static String ARTICLE_FEATURE_GRPC_API;
    public static String ARTICLE_STABLE_MODEL_NAME;
    public static String PREDICT_BATCH_SIZE;
}

But I can't get the value , for eaxmple, RankingConfig.ARTICLE_FEATURE_GRPC_API .

What should I do ?

Upvotes: 0

Views: 625

Answers (2)

Nenad Siler
Nenad Siler

Reputation: 7

Try with

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "app.ranking")
public class RankingConfig {
    public static String UNK;
    public static String ARTICLE_FEATURE_GRPC_API;
    public static String ARTICLE_STABLE_MODEL_NAME;
    public static String PREDICT_BATCH_SIZE;

//and you must have getters and setters!
}

And for the YAML file, I don't think that you need quotation marks ("")

app:
  ranking:
    UNK: "[UNK]"
    ARTICLE_FEATURE_GRPC_API: "0.0.0.0:50055"
    ARTICLE_STABLE_MODEL_NAME" "article_push_stable"
    PREDICT_BATCH_SIZE: 256

Upvotes: 0

Kemal Kaplan
Kemal Kaplan

Reputation: 1024

You have to add getter and setters for your properties. Like;

public void setPREDICT_BATCH_SIZE(String PREDICT_BATCH_SIZE) {
    this.PREDICT_BATCH_SIZE = PREDICT_BATCH_SIZE;
}

public String getPREDICT_BATCH_SIZE() {
    return PREDICT_BATCH_SIZE;
}

Upvotes: 1

Related Questions