如何在POJO类中获得与相同变量名不同的结果变量名

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

How to get different variable name in result instead of getting the same variable name in POJO class

问题

Sure, here's the translated part:

{
   "BackGround":"value"
}
英文:

I am using java and spring boot in my project and I am trying to get the key as "BackGround" instead of "backGround". Could you please help me to get the expected result.

Thanks in advance!

Code:

        @Entity 
        public class EmpEntity{
        
        private String backGround;
        
        }
        

        @Data
        public class EmpPojo{
        
        @JsonProperty("BackGround")
        private String backGround;
        
        }

Impl:

        ObjectMapper obj=new ObjectMapper();
        EmpEntity ent=new EmpEntity();
        ent.setBackGround("value");
        EmpPojo pojo=obj.readValue(new Gson().toJson(ent),EmpPojo.class);

Excepted Output:

        {
        "BackGround":"value"
        }

答案1

得分: 2

@JsonProperty是Jackson注解。对于Gson,你需要使用@SerializedName,它来自包com.google.gson.annotations

@SerializedName("BackGround")
private String backGround;
英文:

@JsonProperty is the Jackson annotaion. For Gson you have to use @SerializedName from package com.google.gson.annotations

    @SerializedName("BackGround")
    private String backGround;

huangapple
  • 本文由 发表于 2023年6月29日 22:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581985.html
匿名

发表评论

匿名网友

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

确定