SpringBoot @JsonProperty的使用属性的名称序列化为另外一个名称

Restful 接口调用Json接收相关问题

1、背景:

在项目上使用SpringBoot为框架,调用第三方接口时,返回的参数类型,不符合标准的命名规则,需要进行处理,接受数据

2、现象:

调用第三方接口返回数据格式为方式均为小写,如下:

          {
            "rowid": "111111",
            "created": "2018-12-27 16:15:25",
            "createdby": "1111111",
            "lastupd": "2018-12-27 08:25:48",
            "lastupdby": "111111",
            "modificationnum": 1
          }

返回Json参数字段均为小写,在接收时,需要按照标准的命名规则进行映射

3、解决办法:

创建接收数据对象,生成Get\Set方法:,在Set方法上,加上@JsonProperty注解,

@JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把rowId属性序列化为rowid,@JsonProperty("rowid")。

    private String rowId;
    private Date created;
    private String createdBy;
    private Date lastUpd;
    private String lastUpdBy;
​
    @JsonProperty("rowId")
    public String getRowId() {
        return rowId;
    }
​
    @JsonProperty("rowid")
    public void setRowId(String rowId) {
        this.rowId = rowId;
    }
​
    public Date getCreated() {
        return created;
    }
    @JsonDeserialize(using = CustomJsonDateDeserializer.class)
    public void setCreated(Date created) {
        this.created = created;
    }
​
    @JsonProperty("createdBy")
    public String getCreatedBy() {
        return createdBy;
    }
​
    @JsonProperty("createdby")
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

以上,就会将数据进行自动映射,获取到数据

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值