Spring Boot使用@JsonProperty,@JsonIgnore,@JsonFormat注解

@JsonProperty, @JsonIgnore 和 @JsonFormat 注解都是 fasterxml jackson 里面的注解,现在也被 Spring Boot 集成了。
这里需要注意的是将对象转换成json字符串使用的方法是fasterxml.jackson提供的!!
如果使用fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
没有生效,为啥?
因为fastjson不认识@JsonProperty注解呀!所以要使用jackson自己的序列化工具方法

我们在使用上面的注解时,不需要在 pom.xml 显示的引入 fasterxml jackson 的依赖包。只需要加入如下依赖即可。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

@JsonProperty
用于属性、setter / getter 方法上,属性序列化后可重命名

@JsonProperty("image_width")
private Double imageWidth;

@JsonProperty("image_height")
private Double imageHeight;

// 为反序列化期间要接受的属性定义一个或多个替代名称,可以与@JsonProperty一起使用
@JsonAlias({"pass_word", "passWord"})
@JsonProperty("pwd")
private String password;

生成的 json 字符串就是image_width和image_height。
@JsonIgnore

属性使用此注解后,将不被序列化。

@JsonFormat
用于格式化日期

@JsonFormat(pattern = "yyyy-MM-dd")
private Date birthday;

//序列化、反序列化时,格式化时间
@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date createDate;

@JsonInclude,@JsonIgnoreProperties,@JsonIgnore

@Data
//序列化、反序列化忽略的属性,多个时用“,”隔开
@JsonIgnoreProperties({"captcha"})
//当属性的值为空(null或者"")时,不进行序列化,可以减少数据传输
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class UserVoByJson {

    // 序列化、反序列化时,属性的名称
//    @JsonProperty("userName")
    private String username;

    // 为反序列化期间要接受的属性定义一个或多个替代名称,可以与@JsonProperty一起使用
//    @JsonAlias({"pass_word", "passWord"})
//    @JsonProperty("pwd")
    private String password;

    //序列化、反序列化时,格式化时间
//    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createDate;

    //序列化、反序列化忽略属性
//    @JsonIgnore
    private String captcha;

}

真实案例
{
"rowid": "111111",
"created": "2018-12-27 16:15:25",
"createdby": "1111111",
"lastupd": "2018-12-27 08:25:48",
"lastupdby": "111111",
"modificationnum": 1
}
返回Json参数字段均为小写,在接收时,需要按照标准的命名规则进行映射

解决办法:

创建接收数据对象,生成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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值