jackson 快速开始
发布日期:2021-06-29 22:24:15
浏览次数:2
分类:技术文章
本文共 4598 字,大约阅读时间需要 15 分钟。
文章目录
jackson 介绍
官方地址: https://github.com/FasterXML/jackson-core
常用使用方法
maven引入
... 2.9.9 com.fasterxml.jackson.core jackson-core ${jackson-version} com.fasterxml.jackson.core jackson-databind ${jackson-version} com.fasterxml.jackson.core jackson-annotations ${jackson-version}
JAVA对象转JSON[JSON序列化]
ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。
ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。
- writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。
- writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。
- writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。
- writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。
JSON转Java类[JSON反序列化]
JSON转对象
objectMapping.readValue(str, ojb.class)
-
如果json字符串中的属性个数小于java对象中的属性个数,可以顺利转换,java中多的那个属性为null
-
如果json字符串中出现java对象中没有的属性,则在将json转换为java对象时会报错:Unrecognized field, not marked as ignorable
demo
public class JacksonTest { public static void main(String args[]) { Student student = new Student(); student.setAge(20); student.setName("she"); ObjectMapper mapper = new ObjectMapper(); try { //Java类转JSON String json = mapper.writeValueAsString(student); System.out.println(json); } catch (JsonProcessingException e) { e.printStackTrace(); } //Java集合转JSON ListstudentList = new ArrayList (); studentList.add(student); String jsonlist = null; try { jsonlist = mapper.writeValueAsString(studentList); } catch (JsonProcessingException e) { e.printStackTrace(); } System.out.println(jsonlist); String json = "{\"name\":\"she\",\"age\":20}"; /** * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。 */ ObjectMapper mapper1 = new ObjectMapper(); Student student1 = null; try { student1 = mapper1.readValue(json, Student.class); } catch (IOException e) { e.printStackTrace(); } System.out.println(student1); } static class Student { private String name; private int age; public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }}
SpringBoot Jackson
SpringBoot JSON工具包默认是Jackson,只需要引入spring-boot-starter-web依赖包,自动引如下相应依赖包:
com.fasterxml.jackson.core jackson-databind -->数据绑定依赖于下面两个包2.8.7 com.fasterxml.jackson.core jackson-annotations -->注解包2.8.0 com.fasterxml.jackson.core jackson-core -->核心包2.8.7
Jackson两种配置方式
- application.properties文件
# 日期格式化spring.jackson.date-format=yyyy-MM-dd HH:mm:ss# 日期时区spring.jackson.time-zone=GMT+8# 返回值null不显示spring.jackson.default-property-inclusion=non_null
- bean配置
import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;@Configurationpublic class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); // 返回值过滤null或""值 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY).setSerializationInclusion(JsonInclude.Include.NON_NULL); return objectMapper; }}
转载地址:https://docker.blog.csdn.net/article/details/105747959 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月21日 12时30分23秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MySQL外键约束的参照操作
2019-04-30
文字过长显示省略号的一样简单解决方法,不用额外的其他框架
2019-04-30
Mysql 修改数据库,添加删除单列,添加删除多列,添加删除约束
2019-04-30
防止表单重复提交的一种方式
2019-04-30
Win10开机画圈圈没完没了?有这招绝对搞定
2019-04-30
Wi-Fi密码记不住怎么办?这条命令就搞定!
2019-04-30
layui日期时间段的设置,开始时间-结束时间
2019-04-30
被锁的U盘怎么抢救资料?
2019-04-30
Mysql的增删改查
2019-04-30
java.sql.SQLException: ORA-01810: 格式代码出现两次
2019-04-30
Linux中rz和sz命令
2019-04-30
ORA-01157 错误解决,原因DBWR锁定产生
2019-04-30
JFinal 表无主键的新增方法
2019-04-30
优秀的程序员和一般的程序员差别在哪
2019-04-30
使用Layui实现数据表格中鼠标悬浮图片放大效果,离开时恢复原图
2019-04-30
window系统桌面图标间距调整
2019-04-30
业务并发需求的实现
2019-04-30
tomcat SSL证书的配置
2019-04-30
Oracle时间戳(毫秒)转为Date
2019-04-30