SpringBoot配置Jackson处理字段
常用框架 阿里fastjson,谷歌gson等
JavaBean序列化为json
- 性能:Jackson>FastJson>Gson>lib 同个结构
- Jackson、Fastjson、Gson等库各有优缺点,各有自己的专长
- 空间换时间,时间换空间
Jackson处理相关自动
- 指定字段不返回:@JsonIgnore
- 指定日期格式:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
- 空字段不返回:@JsonInclude(JsonInclude.Include.NON_NULL)
- 指定别名:@JsonProperty("TITLE")
package net.cyb.demo.domain; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty; import java.io.Serializable;
import java.util.Date;
import java.util.List; public class Video implements Serializable {
private int id;
//设置别名
@JsonProperty("TITLE")
private String title;
private String summary;
//指定字段不返回
@JsonIgnore
private int pricce;
//空字段不返回
@JsonInclude(JsonInclude.Include.NON_NULL)
private String converImg;
//指定日期格式
@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
private Date createTime;
private List<Chapter> chapterList; public List<Chapter> getChapterList() {
return chapterList;
} public void setChapterList(List<Chapter> chapterList) {
this.chapterList = chapterList;
} public Video() {
} public Video(int id, String title) {
this.id = id;
this.title = title;
this.createTime = new Date();
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getSummary() {
return summary;
} public void setSummary(String summary) {
this.summary = summary;
} public int getPricce() {
return pricce;
} public void setPricce(int pricce) {
this.pricce = pricce;
} public String getConverImg() {
return converImg;
} public void setConverImg(String converImg) {
this.converImg = converImg;
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} @Override
public String toString() {
return "Video{" +
"id=" + id +
", title='" + title + '\'' +
", summary='" + summary + '\'' +
", pricce=" + pricce +
", converImg='" + converImg + '\'' +
", createTime=" + createTime +
", chapterList=" + chapterList +
'}';
}
}
序列化操作
public JsonData list() throws JsonProcessingException {
List<Video> list=videoService.listVideo();
ObjectMapper objectMapper=new ObjectMapper();
String jsonStr=objectMapper.writeValueAsString(list);
System.out.println(jsonStr);
return JsonData.buildSuccess(list);
}
反序列化
public JsonData list() throws JsonProcessingException {
List<Video> list=videoService.listVideo();
ObjectMapper objectMapper=new ObjectMapper();
String jsonStr=objectMapper.writeValueAsString(list);
System.out.println(jsonStr);
List<Video> list1 = objectMapper.readValue(jsonStr, List.class);
System.out.println(list1);
return JsonData.buildSuccess(list);
}
SpringBoot配置Jackson处理字段的更多相关文章
- springboot之jackson的两种配置方式
springboot 针对jackson是自动化配置的,如果需要修改,有两种方式: 方式一:通过application.yml 配置属性说明:## spring.jackson.date-format ...
- springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器
1. Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...
- 源码分析springboot自定义jackson序列化,默认null值个性化处理返回值
最近项目要实现一种需求,对于后端返回给前端的json格式的一种规范,不允许缺少字段和字段值都为null,所以琢磨了一下如何进行将springboot的Jackson序列化自定义一下,先看看如何实现,再 ...
- SpringBoot配置Aop笔记【例子】
众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...
- SpringBoot配置属性之MVC
SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...
- SpringBoot 配置提示功能
目的 配置自动提示的辅助功能可以让配置写起来更快,准确率大大提高. springboot jar 包含提供所有支持的配置属性细节的元数据文件.文件的目的是为了让 IDE 开发者在用户使用 applic ...
- Springcloud 中 SpringBoot 配置全集 (收藏版)
Springcloud 中 SpringBoot 配置全集 (收藏版) 疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 前言 疯狂创客圈(笔者尼恩创建的高并发研习社群 ...
- 跨站点脚本编制 - SpringBoot配置XSS过滤器(基于mica-xss)
1. 简介 XSS,即跨站脚本编制,英文为Cross Site Scripting.为了和CSS区分,命名为XSS. XSS是最普遍的Web应用安全漏洞.这类漏洞能够使得攻击者嵌入恶意脚本代码 ...
- SpringBoot——配置及原理
更多内容,前往IT-BLOG 一.Spring Boot全局配置文件 application.properties 与 application.yml 配置文件的作用:可以覆盖 SpringBoot ...
- SpringBoot配置属性之Server
SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...
随机推荐
- iceoryx源码阅读(一)——全局概览
一.什么是iceoryx iceoryx是一套基于共享内存实现的进程间通信组件. 二.源码结构 iceoryx源码包括若干工程,整理如下表所示: 下图展示了主要项目之间的依赖(FROM:iceoryx ...
- Review社区PR的方式
有时候review社区改动较大的PR时,在github上看不太方便,需要把相应的pr,apply到本地,记录下常用的方式 # 添加pr仓库 git remote add ${upstream_name ...
- C语言(较深入原理):%s通过字符串首元素地址输出,用指针数组来作示例
首先,我们输出一个字符串都知道是用%s来输出,但是我们并没有多想是通过什么方式来输出的. 今天我在看指针数组的时候发现了一个问题,按就是定义一个字符类型的指针数组, /*字符串的输出本身就需要他的地址 ...
- SQL Server使用for xml path 多行合并成一行,逗号分隔,拆解分析实现原理
我们写sql脚本处理数据的时候 针对部分数据进行group by 分组,分组后需要将部分数据放入分组后的行里面以逗号分隔. 举一个简单例子: 如上图的数据,需要对学生进行分组,取得学生都参与了哪些学科 ...
- 数据库中存储bool对象的数据,使用Bit和Integer有什么区别,性能上有多大的差异
在数据库中存储布尔(Boolean)值时,常见的两种选择是使用 BIT 类型或 INTEGER 类型.两者在存储.性能和使用上的区别如下: 1. BIT 类型 存储:BIT 类型专门用于存储布尔值.通 ...
- Kubernetes Pod调度:从基础到高级实战技巧
本文深入探讨了Kubernetes中的Pod调度机制,包括基础概念.高级调度技术和实际案例分析.文章详细介绍了Pod调度策略.Taints和Tolerations.节点亲和性,以及如何在高流量情况下优 ...
- kubernets之高级调度
一 节点的污点以及pod的容忍度以及节点的亲缘性对比 1.1 首先需要介绍的是节点的污点以及pod的污点容忍度 污点是节点的属性,容忍度是pod的属性,只有当一个pod的容忍度包含节点的污点,pod才 ...
- Istio(四):创建部署Gateway并使用网关暴露服务
目录 一.模块概览 二.系统环境 三.Gateway网关 3.1 使用 Gateway 四.实战:使用Gateway发布服务 4.1 创建部署并使用网关暴露 4.2 清理 一.模块概览 在Kubern ...
- 提升WordPress网站加载速度的8个小技巧
提升WordPress网站加载速度是至关重要的,它不仅可以提高用户体验,还有助于SEO排名.以下是提升WordPress网站加载速度的8个小技巧,希望能帮助到大家. 优化图片: 使用适当大小和格式的图 ...
- rocketmq 搭建配置
broker组1: # NameServer地址 namesrvAddr=192.168.1.100: 9876;192.168.1.101: 9876 # 集群名称 brokerClusterNam ...