JAVA物件jackson序列化json屬性名首字母變成小寫的解決方案
阿新 • • 發佈:2019-01-04
java程式碼物件如下:
package com.ctrip.market.messagepush.service.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WaitSendModel {
public long MsgID;
public String GroupID;
public int SendLevel;
public int SendType;
public long getMsgID() {
return MsgID;
}
public void setMsgID(long msgID) {
this.MsgID = msgID;
}
public String getGroupID() {
return GroupID;
}
public void setGroupID(String groupID) {
this.GroupID = groupID;
}
public int getSendLevel() {
return SendLevel;
}
public void setSendLevel(int sendLevel) {
this.SendLevel = sendLevel;
}
public int getSendType() {
return SendType;
}
public void setSendType(int sendType) {
this.SendType = sendType;
}
}
執行結果,首字母小寫:
Json={"msgID":100005,"groupID":"00001","sendLevel":5}
以上的物件如果通過jackson轉成json格式的話,首字母會自動變成小寫,如果我想讓首字母變成大寫的,該如何處理呢?
在屬性上加@JsonProperty 註解,並且在對應的setter ,getter 上面加上@JsonIgnore,這樣就可以了,新增完之後的程式碼如下:
package com.ctrip.market.messagepush.service.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WaitSendModel {
@JsonProperty
public long MsgID;
@JsonProperty
public String GroupID;
@JsonProperty
public int SendLevel;
@JsonProperty
public int SendType;
@JsonIgnore
public long getMsgID() {
return MsgID;
}
@JsonIgnore
public void setMsgID(long msgID) {
this.MsgID = msgID;
}
@JsonIgnore
public String getGroupID() {
return GroupID;
}
@JsonIgnore
public void setGroupID(String groupID) {
this.GroupID = groupID;
}
@JsonIgnore
public int getSendLevel() {
return SendLevel;
}
@JsonIgnore
public void setSendLevel(int sendLevel) {
this.SendLevel = sendLevel;
}
@JsonIgnore
public int getSendType() {
return SendType;
}
@JsonIgnore
public void setSendType(int sendType) {
this.SendType = sendType;
}
}
執行結果,首字母大寫:
Json={"MsgID":100005,"GroupID":"00001","SendLevel":5,"SendType":0}