本文为博主原创,未经允许不得转载:

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

jackson的maven依赖

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

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

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<CustomerInfo >转换成json格式的数据传递给前台。但实体类中基本属性字段的值都存储在快照属性字段中。此时我可以在业务层中做处理,

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

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

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)

jackson中@JsonProperty、@JsonIgnore等常用注解总结的更多相关文章

  1. Jackson中@JsonProperty等常用注解

    Java生态圈中有很多处理JSON和XML格式化的类库,Jackson是其中比较著名的一个.虽然JDK自带了XML处理类库,但是相对来说比较低级 本文将介绍的Jackson常用注解:精简概述 Jack ...

  2. Spring框架中Bean管理的常用注解

    1. @Component:组件.(作用在类上)可以作用在任何一个类上 2. Spring中提供@Component的三个衍生注解:(功能目前来讲是一致的) * @Controller -- 作用在W ...

  3. Spring中Bean管理的常用注解

    在Spring中,主要用于管理bean的注解分为四大类:1.用于创建对象.2.用于给对象的属性注入值.3.用于改变作用的范围.4.用于定义生命周期.这几个在开发中经常接触到,也可以说每天都会遇见.其中 ...

  4. 用Jackson进行Json序列化时的常用注解

    Jackson时spring boot默认使用的json格式化的包,它的几个常用注解: @JsonIgnore 用在属性上面,在序列化和反序列化时都自动忽略掉该属性 @JsonProperty(&qu ...

  5. Spring Data JPA 中常用注解

    一.java对象与数据库字段转化 1.@Entity:标识实体类是JPA实体,告诉JPA在程序运行时生成实体类对应表 2.@Table:设置实体类在数据库所对应的表名 3.@Id:标识类里所在变量为主 ...

  6. spring注解开发中常用注解以及简单配置

    一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...

  7. SpringBoot 中常用注解

    本篇博文将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注 ...

  8. SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍

    SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍 本篇博文将介绍几种如何处理url中的参数的注解@PathVaribale/@Requ ...

  9. SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别

    @Controller 处理http请求 @Controller //@ResponseBody public class HelloController { @RequestMapping(valu ...

随机推荐

  1. 飞机躲避炮弹---java

    最近闲来无事,花了一天多的时间写了一个小游戏,虽然说游戏本身很无聊吧,但是自己也从这个过程中学到了许多东西...分享一下. 代码内容自行理解吧... 层次结构: package cn.sxt.game ...

  2. mac sed 使用踩坑实录

    [转自别处] 比如我sed想做文件原地的替换,但是怎么写都出错,错误提示还莫名其妙,后来多方搜索才知道Mac上的sed如果参数有-i就必须加上备份指令,即-i后添加任意字符,那些字符就作为备份文件的后 ...

  3. elk-插件(head、X-pack)(五)

    一.修改ES配置,允许REST跨源操作ES服务器,添加以下2个配置,并重启ES. http.cors.enabled: true #如果启用了 HTTP 端口,那么此属性会指定是否允许跨源 REST ...

  4. linux基本介绍

    Linux介绍 操作系统: 主要作用是管理好硬件设备,并为用户和应用程序提供简单的接口,以便于使用.作为中间人链接软件和硬件. 不同领域的操作系统: 1.桌面操作系统 Windows(用户群大).ma ...

  5. 软件毕业设计文档流程与UML图之间的关系

    每个模型都是用一种或者多种UML图来描述的,映射关系如下: 1.用例模型:使用用例图.顺序图.通信图.活动图和状态图来描述. 2.分析模型:使用类图和对象图(包括子系统和包).顺序图(时序图).通信图 ...

  6. yum命令查看某个命令是由那个包提供的

    [root@linux-node2 ~]$ yum whatprovides fuserLoaded plugins: fastestmirrorLoading mirror speeds from ...

  7. NFC中国-中国第一NFC论坛,NFC中文论坛+NFC技术社区+NFC_电子发烧友网【申明:来源于网络】

    NFC中国-中国第一NFC论坛,NFC中文论坛+NFC技术社区[申明:来源于网络] NFC中国-中国第一NFC论坛,NFC中文论坛:http://nfcchina.org/forum.php NFC技 ...

  8. TZOJ :2731: 存钱计划(二)

    描述 在TZC,WY存了钱,现在他要去买东西了.店很多,标记为1,2,3,4,5,6....但有的店之间有大路相连,而有的没有路.现在要由一个店到另一个店买东西,中途最少要经过多少个其它的店铺呢? 如 ...

  9. SVProgressHUD提示框IOS

    SVProgressHUD--比MBProgressHUD更好用的 iOS进度提示组件 项目里用到SVProgressHud,感觉背景颜色太丑,因为很久很久以前改过,就想在这个项目里也改下,但是时间过 ...

  10. java之IDEA中使用Maven

    Maven的安装与使用 安装 1.下载,官网下载. 2.解压,存放路径中不可包含空格和中文.如:"E:\dev\workspace\maven\apache-maven-3.6.0" ...