JAVA对象jackson序列化json属性名首字母变成小写的解决方案

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}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值