英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论