Jackson常用注解介绍

本文介绍了Jackson库中常用的注解,包括排除属性、属性别名、属性排序等,通过示例详细展示了如何使用这些注解来提高JSON序列化与反序列化的灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接上一篇文章 Json解析工具Jackson(简单应用),jackson在实际应用中给我们提供了一系列注解,提高了开发的灵活性,下面介绍一下最常用的一些注解
1.排除属性
  • @JsonIgnoreProperties

        此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。

  • @JsonIgnore

        此注解用于属性或者方法上(最好是属性上),作用和上面的@JsonIgnoreProperties一样。

2.属性别名

  • @JsonProperty

       此注解用于序列化/反序列化都有效;

3.属性排序

  • @JsonPropertyOrder

       注释在类声明中

4.JSON格式化

  • @JsonFormat

       此注解用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式,比如@JsonFormat(pattern= "yyyy-MM-dd HH-mm-ss")

5.属性格式转换

  • @JsonSerialize 序列化

       此注解用于属性或者getter方法上,用于在序列化时嵌入我们自定义的代码,比如序列化一个double时在其后面限制两位小数点。


  • @JsonDeserialize 反序列化
            注意:在使用hibernate的时候,查询数据库后产生的实体类是个代理类,这时候转换JSON会报错;
解决方法有两种:
1)设置FAIL_ON_EMPTY_BEANS属性,告诉Jackson空对象不要抛异常;
mapper.disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);
2)使用@JsonIgnoreProperties注解
在实体类声明处加上@JsonIgnoreProperties(value ={"hibernateLazyInitializer ", "handler"})注解;
建议使用@JsonIgnoreProperties注解,这样生成的JSON中不会产生多余的字段;

       此注解用于属性或者setter方法上,用于在反序列化时可以嵌入我们自定义的代码,类似于上面的@JsonSerialize


6.父/子关联

  • @JsonManagedReference,放在父亲类中;
  • @JsonBackReference,放在孩子类中;

7.去掉包装

  • @JsonUnwrapped
Ability to map JSON like
{
"name" : "home",
"latitude" : 127,
"longitude" : 345
}
to classes defined as:
class Place {
public String name;

@JsonUnwrapped
public Location location;
}

class Location {
public int latitude, longitude;
}

完整例子

//表示序列化时忽略的属性
@JsonIgnoreProperties(value = { "word" })
public class Person {
    privateString name;
    private intage;
    privateboolean sex;
    private Datebirthday;
    privateString word;
    privatedouble salary;

    publicString getName() {
       returnname;
    }

    public voidsetName(String name) {
       this.name =name;
    }

    public intgetAge() {
       returnage;
    }

    public voidsetAge(int age) {
       this.age =age;
    }

    publicboolean isSex() {
       returnsex;
    }

    public voidsetSex(boolean sex) {
       this.sex =sex;
    }

    public DategetBirthday() {
       returnbirthday;
    }

    //反序列化一个固定格式的Date
   @JsonDeserialize(using = CustomDateDeserialize.class)
    public voidsetBirthday(Date birthday) {
      this.birthday = birthday;
    }

    publicString getWord() {
       returnword;
    }

    public voidsetWord(String word) {
       this.word =word;
    }

    //序列化指定格式的double格式
   @JsonSerialize(using = CustomDoubleSerialize.class)
    publicdouble getSalary() {
       returnsalary;
    }

    public voidsetSalary(double salary) {
       this.salary= salary;
    }

    publicPerson(String name, int age) {
       this.name =name;
       this.age =age;
    }

    publicPerson(String name, int age, boolean sex, Date birthday,
          String word,double salary) {
      super();
       this.name =name;
       this.age =age;
       this.sex =sex;
      this.birthday = birthday;
       this.word =word;
       this.salary= salary;
    }

    publicPerson() {
    }

   @Override
    publicString toString() {
       return"Person [name=" + name + ", age=" + age + ", sex=" + sex
             + ",birthday=" + birthday + ", word=" + word + ", salary="
             + salary +"]";
    }
}


public class Demo {
 

   publicstatic void main(String[] args) {
      writeJsonObject();
       //readJsonObject();
    }

    //直接写入一个对象(所谓序列化)
    publicstatic void writeJsonObject() {
       ObjectMappermapper = new ObjectMapper();
       Personperson = new Person("nomouse", 25, true, new Date(), "程序员",
            2500.0);
       try {
         mapper.writeValue(new File("c:/person.json"), person);
       } catch(JsonGenerationException e) {
         e.printStackTrace();
       } catch(JsonMappingException e) {
         e.printStackTrace();
       } catch(IOException e) {
         e.printStackTrace();
       }
    }

    //直接将一个json转化为对象(所谓反序列化)
    publicstatic void readJsonObject() {
       ObjectMappermapper = new ObjectMapper();

       try {
          Personperson = mapper.readValue(new File("c:/person.json"),
               Person.class);
         System.out.println(person.toString());
       } catch(JsonParseException e) {
         e.printStackTrace();
       } catch(JsonMappingException e) {
         e.printStackTrace();
       } catch(IOException e) {
         e.printStackTrace();
       }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值