常用框架 阿里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处理字段的更多相关文章

  1. springboot之jackson的两种配置方式

    springboot 针对jackson是自动化配置的,如果需要修改,有两种方式: 方式一:通过application.yml 配置属性说明:## spring.jackson.date-format ...

  2. springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器

    1.    Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...

  3. 源码分析springboot自定义jackson序列化,默认null值个性化处理返回值

    最近项目要实现一种需求,对于后端返回给前端的json格式的一种规范,不允许缺少字段和字段值都为null,所以琢磨了一下如何进行将springboot的Jackson序列化自定义一下,先看看如何实现,再 ...

  4. SpringBoot配置Aop笔记【例子】

    众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...

  5. SpringBoot配置属性之MVC

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...

  6. SpringBoot 配置提示功能

    目的 配置自动提示的辅助功能可以让配置写起来更快,准确率大大提高. springboot jar 包含提供所有支持的配置属性细节的元数据文件.文件的目的是为了让 IDE 开发者在用户使用 applic ...

  7. Springcloud 中 SpringBoot 配置全集 (收藏版)

    Springcloud 中 SpringBoot 配置全集 (收藏版) 疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 前言 疯狂创客圈(笔者尼恩创建的高并发研习社群 ...

  8. 跨站点脚本编制 - SpringBoot配置XSS过滤器(基于mica-xss)

    1. 简介   XSS,即跨站脚本编制,英文为Cross Site Scripting.为了和CSS区分,命名为XSS.   XSS是最普遍的Web应用安全漏洞.这类漏洞能够使得攻击者嵌入恶意脚本代码 ...

  9. SpringBoot——配置及原理

    更多内容,前往IT-BLOG 一.Spring Boot全局配置文件 application.properties 与 application.yml 配置文件的作用:可以覆盖 SpringBoot ...

  10. SpringBoot配置属性之Server

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...

随机推荐

  1. Windows远程连接工具有哪些

    Windows远程连接工具,一般称为远程桌面软件,更准确的叫远程访问软件或远程控制软件,可以让你从一台电脑远程控制另一台电脑.远程桌面软件允许您控制连接的计算机,就好像它就在您面前一样. 远程桌面工具 ...

  2. 我的书《Unity3D动作游戏开发实战》出版了

    首先感谢帮助和参与前期检阅的朋友们.本书是我经验积累的提炼,书中既有干货分享也有对基础内容的详解补充. 同时由于是第一次撰写书籍,许多地方仍有不足还请读者朋友们见谅. 在京东或当当等都可以购买到本书: ...

  3. 工业福利!用.NET快速开发物联网扫码器设备的通用扫码功能

    不管你是用.net framework还是用.net core或者更高版本.net环境,这篇文章也许都能帮到你!因为接下来我会提供一个简单粗暴的方式,来快速实现多款扫码器的通用扫码功能.目前本地测试过 ...

  4. 为什么不推荐在Spring Boot中使用@Value加载配置

    @Value注解相信很多Spring Boot的开发者都已经有接触了,通过使用该注解,我们可以快速的把配置信息加载到Spring的Bean中. 比如下面这样,就可以轻松的把配置文件中key为com.d ...

  5. rabbitmq添加延时通道时报错

    rabbitmq添加延时通道时报错 'x-delayed-type' must be an existing exchange type 解决方案: 我实际用的是x-delayed-type:topi ...

  6. 『手撕Vue-CLI』拉取版本号

    开篇 在上一篇文章中,给 nue-cli 添加了与用户终端交互的功能,这一次来实现一个拉取版本号的功能. 这个功能的背景是,有时候我们在使用脚手架的时候,不同版本的脚手架可能会有不同的功能,所以用户有 ...

  7. MySQL学习笔记-函数

    MySQL-常用函数 select {函数}({参数}); select是查询用的,用来展示函数返回值. 一. 字符串函数 常用的字符串函数: 1. concat 拼接 select concat(' ...

  8. k8s——集群环境问题合集

    创建集群 k8s集群创建 集群环境问题合集 重置master节点 kubeadm reset -f # -f 强制重置 可选 重置node节点 # 当你的master节点重置后,node节点需要重新加 ...

  9. 在AngularJS中,控制器没有生命周期方法

    在AngularJS中,控制器没有生命周期方法,但是$scope对象有一些事件,可以模拟生命周期方法的行为.例如,$scope.$on('$destroy', function() {...})可以在 ...

  10. ETL工具-nifi干货系列 第七讲 处理器JoltTransformJSON(续)

    第六讲教程只简单介绍了Jolt的chain转换模式,本节课介绍下Jolt的各种转换模式. 点击的处理器JoltTransformJSON高级配置选项,进行测试Jolt的转换模式. 1.Cardinal ...