jackson使用@JsonProperty注解与fastjson使用@JSONField注解他们修改对象属性对应的序列化键的不同

jackson使用@JsonProperty注解

来源: @JsonPrpperty是jackson包下的一个注解,详细路径 (com.fasterxml.jackson.annotation.JsonProperty;)

作用: @JsonProperty用在属性上,将属性名称序列化为另一个名称

简单的说,就是在给实体类属性名起别名,应用在不同的场合

jackson使用@JsonIgnore的作用

作用:在json序列化时将java 中的一些属性忽略掉,序列化和反序列化都受影响

它的作用就是在后端给前端数据的时候对后端发送出的json字符串能够发挥作用的一个注解,可以忽略不想传递给前台的属性或接口

@Data
@Accessors(chain = true)
@ApiModel("测试实体")
public class Charge  implements Serializable {

    @ApiModelProperty(value = "", required = true, dataType = "String")
    @JsonProperty("wmid")
    private String wmId;

    @ApiModelProperty(value = "获取方式", required = true, dataType = "String")
    @JsonProperty("gathertype")
    private String gatherType;


    @ApiModelProperty(value = "新区类型", required = true, dataType = "String")
    @JsonProperty("invoicetype")
    private String invoiceType;


    @ApiModelProperty(value = "状态", required = true, dataType = "String")
    @JsonIgnore
    private String statue;


}

序列化以及参数输出:

        //序列化
        String writeValueAsString = new ObjectMapper().writeValueAsString(request);
        System.out.println("=============jackson>"+writeValueAsString);

控制台输出:

            "invoicetype": 1,
            "wmId":"1",
            "gathertype": "A",
            

fastjson使用@JSONField注解修改对象属性对应的序列化键的不同

@JSONField是fastjson的一个注解,在fastjson解析一个类为Json对象时,作用到类的每一个属性(field)上。

    通过用@JSONField注解一个类的属性,我们可以达到以下目标
    指定field对应的key名称:@JSONField(name = “NaMe”)
    对于时间字段,指定其格式:@JSONField(format = “yyyy-MM-dd HH:mm:ss”)
    不序列化某些字段:@JSONField(serialize = false)
    指定顺序:@JSONField(ordinal = 1)

@Data
@Accessors(chain = true)
@ApiModel("测试实体")
public class Charge  implements Serializable {

    @ApiModelProperty(value = "", required = true, dataType = "String")
    @@JSONField("wmid")
    private String wmId;

    @ApiModelProperty(value = "获取方式", required = true, dataType = "String")
    @@JSONField("gathertype")
    private String gatherType;


    @ApiModelProperty(value = "新区类型", required = true, dataType = "String")
    @@JSONField("invoicetype")
    private String invoiceType;


}

序列化以及参数输出

        //序列化
        String p = JSONObject.toJSONString(param, SerializerFeature.WriteMapNullValue);
        System.out.println("=============jackson>"+ p);

控制台输出:

            "invoicetype": 1,
            "wmid": 36482.88,
            "gathertype": "A",

:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值