Java中使用fastjson設置字段不序列化
fasetjson設置字段不序列化
alibaba的fasetjson可以設置字段不序列化
使用@JSONField注解的serialize屬性,該屬性默認是可以序列化的,設置成false就表示不可序列化:
使用方式就是在定義字段前加上@JSONField(serialize = false)
比如下面的實體類:
import com.alibaba.fastjson.annotation.JSONField; import java.util.Objects; public class Employee { @JSONField(serialize=false) private String name; private int age; private double salary; public Employee(String name, int age, double salary) { this.name = name; this.age = age; this.salary = 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 double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Employee employee = (Employee) o; return age == employee.age && Double.compare(employee.salary, salary) == 0 && Objects.equals(name, employee.name); } @Override public int hashCode() { return Objects.hash(name, age, salary); } @Override public String toString() { return "Employee{" + "name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}'; } }
使用Json進行序列化:
import com.alibaba.fastjson.JSON; import org.junit.Test; public class EmployeeTest { @Test public void serialTest(){ Employee employee=new Employee("aa",11,2000d); System.out.println(JSON.toJSONString(employee)); } }
可以看到序列化之后的字符串沒有name字段。
到此這篇關于Java中使用fastjson設置字段不序列化的文章就介紹到這了,更多相關fastjson設置字段不序列化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot FeignClient 如何捕獲業(yè)務異常信息
這篇文章主要介紹了Spring Boot FeignClient 如何捕獲業(yè)務異常信息的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06java使用POI批量導入excel數(shù)據(jù)的方法
這篇文章主要為大家詳細介紹了java使用POI批量導入excel數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07java開發(fā)web前端cookie session及token會話機制詳解
如果把人體比作一個web系統(tǒng)的話,cookie、session和token就好像人體的經(jīng)絡和血管一樣,而web系統(tǒng)中的數(shù)據(jù),就好像人體的血液一樣。血液依靠著血管在人體內(nèi)流動,就如數(shù)據(jù)根據(jù)cookie和session機制在web系統(tǒng)中流動一樣2021-10-10commons fileupload實現(xiàn)文件上傳的實例代碼
這篇文章主要介紹了commons fileupload實現(xiàn)文件上傳的實例代碼,包括文件上傳的原理分析等相關知識點,本文給大家介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起看看吧2016-10-10基于spring+quartz的分布式定時任務框架實現(xiàn)
在Spring中的定時任務功能,最好的辦法當然是使用Quartz來實現(xiàn)。這篇文章主要介紹了基于spring+quartz的分布式定時任務框架實現(xiàn),有興趣的可以了解一下。2017-01-01