英文:
Using Jackson and Lombok, is there way to map json fields to static variables
问题
I have to read the JSON file and inject environment property as a static variable.
Here is the JSON
{
"env":"staging",
"index": "test",
"indval": "testVal"
}
I need to map only env field as a static string variable in my Class using Jackson and Lombok
@Data
public class PubConf {
public static String env;
private String index;
private String indval;
}
I always get the following error for the above class:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "env"
Appreciate your thoughts for resolving this.
英文:
I have to read the JSON file and inject environment property as a static variable.
Here is the JSON
{
"env":"staging",
"index": "test",
"indval": "testVal"
}
I need to map only env field as static string variable in my Class using Jackson and Lombok
@Data
public class PubConf {
public static String env;
private String index;
private String indval;
}
I always getting below error for the above class
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "env"
Appreciate your thoughts for resolving this
答案1
得分: 0
@Data
public class PubConf {
private String env;
private String index;
private String indval;
@JsonIgnore
public static String statEnv;
public void setEnv(String env) {
PubConf.statEnv = env;
}
}
Source: https://www.baeldung.com/spring-inject-static-field
英文:
@Data
public class PubConf {
private String env;
private String index;
private String indval;
@JsonIgnore
public static String statEnv;
public void setEnv(String env) {
PubConf.statEnv= env;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论