首页 > 其他分享 >springboot整合Jackson

springboot整合Jackson

时间:2022-12-28 15:37:18浏览次数:61  
标签:jackson springboot eg 整合 Jackson 序列化 ObjectMapper 属性

springboot整合Jackson

Jackson简介

Jackson是一套适合java的数据处理工具,用于JSON格式数据的解析与生成,支持多种类型,是SpringMVC内置解析器。

除了Jackson,常用的JSON解析框架还有GSON(由Google提供的开源库)、FastJSON(由Alibaba提供的开源库)。

引入依赖

直接引入spring-boot-starter-web依赖即可

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

Jackson的核心模块

  • jackson-core:定义底层流的API,包含特殊JSON格式的实现
  • jackson-annotations:包含jackson标准的注解
  • jackson-databind:支持在jackson-core包的基础上实现数据绑定与对象序列化,它依赖于jackson-core包与jackson-annotions包

Jackson的全局配置

Jackson可以直接在配置文件中进行参数配置

spring:
  jackson:
    ## 按照此规则进行对象的序列化
    ## 1.ALWAYS默认 2. NON_NULL属性为NULL不序列化 3.NON_EMPTY属性为""或NULL都不序列化,返回的json没有这个字段
    ## 4.NON_DEFAULT属性为默认值不序列化
    default-property-inclusion: non_null
    ## 指定日期格式
    date-format: yyyy-MM-dd HH:mm:ss
    ## 指定使用语言 zh_CN简体中文
    locale: zh_CN
    ## 指定日期格式化的时区 中国为东8区
    time-zone: GMT+8
    ## 指定属性名的命名方式:SNAKE_CASE蛇形
    property-naming-strategy: SNAKE_CASE

Jackson的常用注解

注解 含义
@JsonIgnoreProperties 作用于类,用法1:在序列化与反序列化中忽略xx属性 eg :@JsonIgnoreProperties({"xx"}) 用法2:忽略不认识的字段 eg: @JsonIgnoreProperties(ignoreUnknown = true)
@JsonIgnore 作用于属性,使相应字段不进行序列化与反序列化
@JsonInclude 作用于类,用于设置序列化方式 eg: @JsonInclude(JsonInclude.Include.NON_EMPTY) 表示属性为""或者为NULL时不进行序列化
@JsonFormat 作用于属性,可以把Date类型直接转化为设定好的格式 eg: @JsonFormat(timezone = "GMT+8" pattern = "yyyy-MM-dd HH-mm-ss")
@JsonProperty 作用于属性,为属性设置别名,在序列化时,把属性名转换为别名 eg: @JsonProperty("file_id")
@JsonAlias 作用于属性,为属性设置别名,在反序列化时,让属性接收多个json字段的值 eg: @JsonAlias({"fileId","file_id"})
@JsonDeserialize 作用于属性,为该属性指定自定义反序列化类 eg: @JsonDeserialize(using = xx.class) 此类需要继承JsonDeserializer<> 重写反序列化方法 public T deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException
@JsonSerialize 作用于属性,为该属性指定自定义序列化类 eg: @JsonSerialize(using = xx.class) 此类需要继承JsonDeserializer<> 重写序列化方法 public void serialize(Boolean aBoolean, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException

Jackson使用ObjectMapper进行序列化与反序列化

ObjectMapper:jackson最常用的API,可以将JSON映射到java对象(反序列化),java对象映射到JSON(序列化)

创建ObjectMapper

正常创建:
ObjectMapper mapper = new ObjectMapper();
在springboot中自动配置会将ObjectMapper注入到spring环境中,因此采用注解引入即可
@Resource
private ObjectMapper mapper;

序列化与反序列化

        //新建ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
        User user1 = new User();
        //序列化
        String json = mapper.writeValueAsString(user1);
        //反序列化
        User user2 = mapper.readValue(json,User.class);
        //建议用这种方法进行反序列化
        User user3 = mapper.readValue(json, new TypeReference<User>() {
        });

参考网址

Jackson官网:https://github.com/FasterXML/jackson

https://juejin.cn/post/6844904166809157639#heading-2

标签:jackson,springboot,eg,整合,Jackson,序列化,ObjectMapper,属性
From: https://www.cnblogs.com/shenStudy/p/17010236.html

相关文章

  • SpringBoot高级篇搜索之Solr环境搭建与简单测试
    搜索可以说是非常常见的场景了,一般选择比较多的有solr和es,底层都是基于Lucene搜索引擎实现。之前简单的使用过solr,一直没有成体系的学习过,正好需要给一个内部项目封装统一的......
  • spring-boot 整合redis作为数据缓存
     添加依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId></dependency>......
  • SpringBoot MultipartFile 上传文件null
    开发环境Springboot1.5.2使用方法,一开始直获取文件为null,不加required=false接口无法访问@PostMapping("/import")publicvoidimportTest(@RequestParam(v......
  • tomcat7运行springboot war包项目启动报错
    报错的log如下:2022-12-2801:11:22.701WARN39756---[localhost-startStop-1]ConfigServletWebServerApplicationContext:Exceptionencounteredduringcontext......
  • 预测型项目管理-4-项目整合管理Ⅰ
    ★★项目整合管理包括对隶属于项目管理过程组的各种过程和项目管理活动进行识别、定义、组合、统一和协调的各个过程。★有效的项目整合通常需要强调对主要交接点进行有效......
  • 预测型项目管理-4-项目整合管理Ⅱ
    4.2制定项目管理计划(规划)❤❤❤★制定项目管理计划是定义、准备和协调项目计划的所有组成部分,并把它们整合为一份综合项目管理计划的过程。主要作用是,生成一份综合文件,用于......
  • 基于springboot+mybatis+mysql+html实现校园宿舍管理系统
    @目录一、系统简介二、系统主要功能界面三、其它系统四、源码下载一、系统简介本系统功能模块主要分为:信息浏览浏览功能、宿舍打卡浏览功能、学生提交信息功能、宿舍搜索......
  • SpringBoot - 注入原生注解 Servlet,Filter,Listener
    @ServletComponentScan(basePackages=“com.atguigu.admin”)指定原生Servlet组件都放在那里@WebServlet(urlPatterns=“/my”)直接响应,没有经过Spring的拦截器@WebF......
  • SpringBoot vue
    springboot整合vue就行前后端完全分离,监听器,过滤器,拦截器https://github.com/ninuxGithub/spring-boot-vue-separateAblogbuiltupwithSpringBootinthebackend......
  • SpringBoot - 实现AOP与声明式事务
    1.实现声明式事务低版本在启动类(@SpringBootApplication)上加上@EnableTransactionManagement注解@EnableTransactionManagement注解其实在大多数情况下,不是必须的,因为S......