SpringBoot 序列化器注解使用方法

Json解析工具Jackson

  • @JsonIgnoreProperties

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

  • @JsonIgnore

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

  • @JsonFormat

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

  • @JsonSerialize

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

public class CustomDoubleSerialize extends JsonSerializer<Double> {  
  
    private DecimalFormat df = new DecimalFormat("##.00");  
  
    @Override  
    public void serialize(Double value, JsonGenerator jgen,  
            SerializerProvider provider) throws IOException,  
            JsonProcessingException {  
  
        jgen.writeString(df.format(value));  
    }  
}  
  • @JsonDeserialize

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

public class CustomDateDeserialize extends JsonDeserializer<Date> {  
  
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  
    @Override  
    public Date deserialize(JsonParser jp, DeserializationContext ctxt)  
            throws IOException, JsonProcessingException {  
  
        Date date = null;  
        try {  
            date = sdf.parse(jp.getText());  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }  
        return date;  
    }  
}  

完整例子:

//表示序列化时忽略的属性  
@JsonIgnoreProperties(value = { "word" })  
public class Person {  
   private String name;  
   private int age;  
   private boolean sex;  
   private Date birthday;  
   private String word;  
   private double salary;  
 
   public String getName() {  
       return name;  
   }  
 
   public void setName(String name) {  
       this.name = name;  
   }  
 
   public int getAge() {  
       return age;  
   }  
 
   public void setAge(int age) {  
       this.age = age;  
   }  
 
   public boolean isSex() {  
       return sex;  
   }  
 
   public void setSex(boolean sex) {  
       this.sex = sex;  
   }  
 
   public Date getBirthday() {  
       return birthday;  
   }  
 
   // 反序列化一个固定格式的Date  
   @JsonDeserialize(using = CustomDateDeserialize.class)  
   public void setBirthday(Date birthday) {  
       this.birthday = birthday;  
   }  
 
   public String getWord() {  
       return word;  
   }  
 
   public void setWord(String word) {  
       this.word = word;  
   }  
 
   // 序列化指定格式的double格式  
   @JsonSerialize(using = CustomDoubleSerialize.class)  
   public double getSalary() {  
       return salary;  
   }  
 
   public void setSalary(double salary) {  
       this.salary = salary;  
   }  
 
   public Person(String name, int age) {  
       this.name = name;  
       this.age = age;  
   }  
 
   public Person(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;  
   }  
 
   public Person() {  
   }  
 
   @Override  
   public String toString() {  
       return "Person [name=" + name + ", age=" + age + ", sex=" + sex  
               + ", birthday=" + birthday + ", word=" + word + ", salary="  
               + salary + "]";  
   }  
 
}  
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,818评论 19 139
  • JAVA序列化机制的深入研究 对象序列化的最主要的用处就是在传递,和保存对象(object)的时候,保证对象的完整...
    时待吾阅读 13,753评论 0 24
  • 2017年8月21日 我原本只想简单记录一下springboot中应用Jpa的简单操作。不想由于hibernate...
    行者N阅读 11,613评论 0 23
  • JPA的注解 1.@MappedSuperclass1)@MappedSuperclass注解只能标准在类上:@T...
    廖马儿阅读 6,554评论 0 2
  • 帝释天传经,天龙八部参与听法。凡界人龙化为男身,现佛之相,迦楼罗大鹏展翅,以龙为食,穷追不舍。然一波未平,一波又起...
    次花小开阅读 1,324评论 0 0

友情链接更多精彩内容