栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot:Jackson的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot:Jackson的使用

文章目录
  • springboot:Jackson的使用
    • 一、全局时间配置
    • 二、使用@JsonFormat为某个属性设置序列化方式
    • 三、使用@JsonPropertyOrder调整属性的序列化顺序
    • 四、使用@JsonProperty修改属性名称
    • 五、使用@JsonInclude使属性值为null不参与序列化
    • 六、使用@JsonIgnore使某个属性不参与序列化
    • 七、jackjson的常用操作

springboot:Jackson的使用 一、全局时间配置

application.properties中配置

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

实体类

@Data
@Builder
public class Model {
    private Integer id;
    private int age;
    private String name;
    private Date createTime;
}

测试controller

	@GetMapping("/jackson/type1/res")
    public Model res() {
        return Model.builder().id(1).age(12).name("zhangsan").createTime(new Date()).build();
    }

测试结果

二、使用@JsonFormat为某个属性设置序列化方式

实体

@Data
@Builder
public class Model {
    private Integer id;
    private int age;
    private String name;
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
    private Date createTime;
}

测试controller

    @GetMapping("/jackson/type1/res2")
    public Model res2() {
        return Model.builder().id(1).age(12).name("zhangsan").createTime(new Date()).build();
    }

测试结果

三、使用@JsonPropertyOrder调整属性的序列化顺序

实体

@Data
@Builder
@JsonPropertyOrder(value={"name", "age"})
public class Model {
    private Integer id;
    private int age;
    private String name;
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
    private Date createTime;
}

测试结果

四、使用@JsonProperty修改属性名称

实体类

@Data
@Builder
@JsonPropertyOrder(value={"name", "age"})
public class Model {
    private Integer id;
    private int age;
    @JsonProperty("myName")
    private String name;
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
    private Date createTime;
}

测试结果

五、使用@JsonInclude使属性值为null不参与序列化

实体类

@Data
@Builder
@JsonPropertyOrder(value={"name", "age"})
public class Model {
    @JsonInclude(value= JsonInclude.Include.NON_NULL)
    private Integer id;
    private int age;
    @JsonProperty("myName")
    private String name;
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
    private Date createTime;
}

测试controller

    @GetMapping("/jackson/type1/res3")
    public Model res3() {
        return Model.builder().age(12).name("zhangsan").createTime(new Date()).build();
    }

测试结果

六、使用@JsonIgnore使某个属性不参与序列化

实体类

@Data
@Builder
@JsonPropertyOrder(value={"name", "age"})
public class Model {
    @JsonInclude(value= JsonInclude.Include.NON_NULL)
    private Integer id;
    @JsonIgnore
    private int age;
    @JsonProperty("myName")
    private String name;
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
    private Date createTime;
}

测试结果

七、jackjson的常用操作

1.获取ObjectMapper对象:jackjson使用ObjectMapper对象进行对象和json格式数据的转换

public ObjectMapper mapper = new ObjectMapper();

2.json格式字符串转化为java对象

mapper.readValue(json, tClass);//第一个参数为json格式的字符串,第二个参数为转换的java对象.class

3.json格式字符串转换为LIst

mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, eClass));
//json是json格式字符串,eClass参数是LIst集合中的元素类.class

4.json格式字符中转换为Map

mapper.readValue(json, mapper.getTypeFactory().constructMapType(Map.class, kClass, vClass));

5.java对象转为json格式字符串

mapper.writeValueAsString(obj);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/946571.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号