JSONField 注解的使用

package com.example.springboot;

import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;

import java.util.Date;

public class TestJsonField {
    @Test
    /**
     * @JsonField 注解
     * 该注解作用于方法上,字段上和参数上,可在序列化和反序列化时进行特性功能定制
     */
    public void testJsonField(){
        Student student = new Student();
        student.setName("王五");
        student.setSex("男");
        student.setTel("4561895656");
        Date date = new Date();
        student.setBirthday(date);
        student.setFlag(true);
        String jsonString = JSON.toJSONString(student);
        System.out.println(jsonString);
    }
}
package com.example.springboot;

import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.format.annotation.DateTimeFormat;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @JsonField 注解
 * 该注解作用于方法上,字段上和参数上,可在序列化和反序列化时进行特性功能定制
 */
public class Student {
    //@jsonfield 注解属性name, 指定序列化后的名字
    //@jsonfield 注解属性 ordinal,指定序列化后的字段顺序, 属性值越小,顺序靠前
    //{"birthday":"2022-04-08","flag":true,"studentName":"王五","sex":"男"}
    @JSONField(name="studentName",ordinal = 1)
    private String name;
    @JSONField(ordinal = 2)
    private String sex;

    //@JSONField 注解属性 serialize ,指定是否序列化该字段
    //{"birthday":"2022-04-08","flag":true,"studentName":"王五","sex":"男"}
    @JSONField(serialize = false)
    private String tel;
    private Integer age;

    //JSONField 注解属性 format指定序列化后的格式
    //{"birthday":"2022-04-08","flag":true,"studentName":"王五","sex":"男"}
    @JSONField(format = "YYYY-MM-dd")
    private Date birthday;
    private boolean flag;

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", tel='" + tel + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", flag=" + flag +
                '}';
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值