jackson中@JsonProperty、@JsonIgnore等常用注解总结

简介: jackson中@JsonProperty、@JsonIgnore等常用注解总结

最近用的比较多,把json相关的知识点都总结一下,jackjson的注解使用比较频繁,

jackson的maven依赖

<groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
<version>2.5.3</version>


在这单独总结一下,最近常用到的注解。

1.@JsonProperty :此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化为name,@JsonProperty("name")。

对属性名称重命名,比如在很多场景下Java对象的属性是按照规范的驼峰书写,但在数据库设计时使用的是下划线连接方式,此处在进行映射的时候

就可以使用该注解。

例如:使用该注解将以下表结构转化为Javabean:

public class CustomerInfo{


private int id;
//使用 @JsonProperty注解将表结构中的字段映射到实体类中
@JsonProperty("customer_name")
private String customerName;

@JsonProperty("customer_id")
private String customerId;

@JsonProperty("product_id")
private String productId;

@JsonProperty("source_address")
private String sourceAddress;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getCustomerName() {
    return customerName;
}

public void setCustomerName(String customerName) {
    this.customerName = customerName;
}

public String getCustomerId() {
    return customerId;
}

public void setCustomerId(String customerId) {
    this.customerId = customerId;
}

public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    this.productId = productId;
}

public String getSourceAddress() {
    return sourceAddress;
}

public void setSourceAddress(String sourceAddress) {
    this.sourceAddress = sourceAddress;
}

}
2.@JsonIgnore此注解用于属性或者方法上(最好是属性上),用来完全忽略被注解的字段和方法对应的属性,即便这个字段或方法可以被自动检测到或者还有其

他的注解,一般标记在属性或者方法上,返回的json数据即不包含该属性。

使用情景:需要把一个List转换成json格式的数据传递给前台。但实体类中基本属性字段的值都存储在快照属性字段中。此时我可以在业务层中做处理,

把快照属性字段的值赋给实体类中对应的基本属性字段。最后,我希望返回的json数据中不包含这两个快照字段,那么在实体类中快照属性上加注解@JsonIgnore,

那么最后返回的json数据,将不会包含customerId和productId两个属性值。

public class CustomerInfo {


private int id;

//使用 @JsonIgnore注解在生成json数据时,忽略该字段

private String customerName;

@JsonIgnore
private String customerId;

@JsonIgnore
private String productId;

private String sourceAddress;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getCustomerName() {
    return customerName;
}

public void setCustomerName(String customerName) {
    this.customerName = customerName;
}

public String getCustomerId() {
    return customerId;
}

public void setCustomerId(String customerId) {
    this.customerId = customerId;
}

public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    this.productId = productId;
}

public String getSourceAddress() {
    return sourceAddress;
}

public void setSourceAddress(String sourceAddress) {
    this.sourceAddress = sourceAddress;
}

}

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

4.@JsonFormat此注解用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式。

例子:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss")

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date updateTime;
5.@JsonSerialize此注解用于属性或者getter方法上,用于在序列化时嵌入我们自定义的代码,比如序列化一个double时在其后面限制两位小数点。

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

7.@JsonInclude 属性值为null的不参与序列化。例子:@JsonInclude(Include.NON_NULL)

目录
相关文章
|
设计模式 算法 Java
Springboot 使用设计模式- 策略模式
Springboot 使用设计模式- 策略模式
1164 0
Springboot 使用设计模式- 策略模式
|
消息中间件 网络协议 Unix
Posix API 与 网络协议栈 详细介绍
Posix API 与 网络协议栈 详细介绍
442 0
|
分布式计算 并行计算 数据库
Schedulerx2.0分布式计算原理&最佳实践
1. 前言 Schedulerx2.0的客户端提供分布式执行、多种任务类型、统一日志等框架,用户只要依赖schedulerx-worker这个jar包,通过schedulerx2.0提供的编程模型,简单几行代码就能实现一套高可靠可运维的分布式执行引擎。
26992 2
|
监控 负载均衡 Java
5 大 SpringCloud 核心组件详解,8 张图彻底弄懂
本文图文详解 Spring Cloud 的五大核心组件,帮助深入理解和掌握微服务架构。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
5 大 SpringCloud 核心组件详解,8 张图彻底弄懂
|
关系型数据库 MySQL 数据库连接
MySQL 1040 - Too many connections 如何解决?
【10月更文挑战第11天】MySQL 1040 - Too many connections 如何解决?
1331 1
|
关系型数据库 MySQL Nacos
nacos启动报错 load derby-schema.sql error
这篇文章描述了作者在使用Nacos时遇到的启动错误,错误提示为加载derby-schema.sql失败,作者通过将数据库从Derby更换为MySQL解决了问题。
nacos启动报错 load derby-schema.sql error
|
负载均衡 监控 Java
SpringCloud常见面试题(一):SpringCloud 5大组件,服务注册和发现,nacos与eureka区别,服务雪崩、服务熔断、服务降级,微服务监控
SpringCloud常见面试题(一):SpringCloud 5大组件,服务注册和发现,nacos与eureka区别,服务雪崩、服务熔断、服务降级,微服务监控
26753 8
SpringCloud常见面试题(一):SpringCloud 5大组件,服务注册和发现,nacos与eureka区别,服务雪崩、服务熔断、服务降级,微服务监控
|
数据采集 存储 人工智能
cdga|数据治理:应对核心业务数据质量参差不齐的挑战与策略
数据治理是指通过制定并实施一系列政策、流程和技术手段,确保数据的可用性、完整性、准确性和安全性,以支持企业的决策和业务运营。对于核心业务数据质量参差不齐的问题,数据治理的重要性不言而喻
|
存储 缓存 算法
Python中的代码优化
【8月更文挑战第2天】Python虽简洁强大,但在处理大数据或高性能需求时可能遇到效率挑战。本文介绍13种Python代码优化技巧,包括选用高效数据结构、避免不必要循环、利用生成器、并发编程、第三方库、内置函数、结果缓存、数据序列化、编译优化、延迟计算、内存管理及性能分析工具等,配以示例代码,助您提升程序性能。
|
SQL XML Java
解决Spring Boot项目中的数据库迁移问题
解决Spring Boot项目中的数据库迁移问题