使用Jackson和Lombok,是否有一种方法将JSON字段映射到静态变量?

huangapple go评论132阅读模式
英文:

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;
    }
}

Source: https://www.baeldung.com/spring-inject-static-field

huangapple
  • 本文由 发表于 2020年7月22日 22:42:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63036917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定